LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-25-2010, 05:43 AM   #1
Ahmad Mujtaba
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
Why is my program giving me segmentation fault?


My program is to find the values of variables using matrices and in matrices using GAUSS ELIMINATION method. It gives segmentation fault then I input the values of coefficients of the variables.





#include<stdio.h>
void main()
{
int n;
int i,j,l;
float m;

printf ("Enter the no. of variables: ");
scanf ("%d", &n);
j=n;

float a[n][n+1], k[j];

printf ("Enter the co-efficients of the variables: ");

for (i=0;i<n;i++)
for (j=0;j<=n;j++)
scanf ("%f", &a[i][j]);

for (i=0;i<n;i++)
{
for (j=0;j<=n;j++)
if (i==j)
m=a[i][j];

for (j=0;j<=n;j++)
a[i][j]=a[i][j]/m;

for (j=0;j<=n;j++)
k[j]=a[i][j];

for (i=1;i<n;i++)
for (j=0;j<=n;j++)
a[i][j]=a[i][j]-(a[i][j]*k[j]);
}

for (i=(n-1);i>=0;i++)
{
for (j=0;j<=n;j++)
k[j]=a[i][j];

for (l=(n-2);l>=0;l--)
for (j=0;j<=n;j++)
{
if (l>=i)
continue;
a[l][j]=a[l][j]-(a[l][j]*k[j]);
}
}

for (i=0;i<n;i++)
for (j=0;j<=n;j++)
if (i==j)
printf ("The value of %dth variable is %f", (i+1), a[i][j]);
}
 
Old 12-25-2010, 06:41 AM   #2
imagine_me2
Member
 
Registered: Nov 2009
Location: Kolkata, India
Distribution: Fedora 11
Posts: 136

Rep: Reputation: 22
you cannot do this "float a[n][n+1], k[j];"... that is declare a variable using a variable size..
try this "float a[50][50], etc.." .. use constant size for arrays.. if you want dynamic allocation use "malloc" .
 
Old 12-25-2010, 07:06 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
It's a good idea to use [CODE] tags to lay out your code nicely when posting to LQs, and also to use a comment to indicate where in your programme the error occurs

As imagine_me2 said, using malloc is a good idea:
Code:
#include <stdlib.h>

float** a = malloc(n*sizeof(float *));
int i;
for(i=0; i < n; i++){
    a[i] = malloc((n+1)*sizeof(float));
}
malloc() reserves memory for a variable and returns a pointer to that memory.
 
Old 12-25-2010, 08:06 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,144

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by Ahmad Mujtaba View Post
It gives segmentation fault then I input the values of coefficients of the variables.
It gives a seg fault after you input the values of coefficients.

Quote:
for (i=1;i<n;i++)
Did you notice that loop over i is inside an earlier loop over i?
What did you intend for that to do?

I don't think it causes a seg fault, but I don't think it does what you intended. It certainly does the wrong thing for Gaussian elimination.

Quote:
for (i=(n-1);i>=0;i++)
But that error does cause a seg fault.

Quote:
Originally Posted by imagine_me2 View Post
you cannot do this "float a[n][n+1], k[j];"... that is declare a variable using a variable size.
But since the compiler didn't complain (remember the problem is a seg fault) Ahmad is using a version of C in which you can declare an automatic (stack local) array using a variable size.

Quote:
Originally Posted by Snark1994 View Post
It's a good idea to use [CODE] tags to lay out your code nicely when posting to LQs,
Yes. I answered anyway, but that should not be considered an endorsement of Ahmad's failure to use [CODE] tags.

Quote:
and also to use a comment to indicate where in your programme the error occurs
I assume that if he knew where the seg fault was, he wouldn't have needed to ask the question.

It is a good idea to either use a debugger or add some printfs to the code so you would know where the seg fault was and you wouldn't need to ask for help.

Quote:
As imagine_me2 said, using malloc is a good idea:
I disagree. That part of the original code was better the way it was. Changing it just complicates things and distracts from the actual problems.

BTW:
Code:
for (j=0;j<=n;j++)
if (i==j)
m=a[i][j];
That chunk of code does its intended job, but did you really want to waste all those CPU cycles doing a trivial step in a hard way?
Also, are you sure you have a matrix in which it is safe to assume a[i][j] would never be zero at that point?

Last edited by johnsfine; 12-25-2010 at 08:42 AM.
 
Old 12-25-2010, 08:16 AM   #5
imagine_me2
Member
 
Registered: Nov 2009
Location: Kolkata, India
Distribution: Fedora 11
Posts: 136

Rep: Reputation: 22
@John My bad.. sorry overlooked it.

@Ahmad Why dont u run the binary through gdb and see where u get a SIGSEGV and post the result. We will then be sure.
 
Old 12-25-2010, 08:33 AM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,144

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by imagine_me2 View Post
@Ahmad Why dont u run the binary through gdb and see where u get a SIGSEGV and post the result.
This time, I already told him the answer. For next time, I think learning gdb might be a bit of a leap for someone still missing very basic concepts of C programming. I think adding printfs might be a better debugging method.

In case you missed my answer earlier to where the SIGSEGV is, I'll explain it more clearly:

Code:
for (i=(n-1);i>=0;i++)
 {
    for (j=0;j<=n;j++)
        k[j]=a[i][j];
The error I marked in red eventually (not on the first pass) causes the code I marked in purple to seg fault.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php https wrapper giving Segmentation fault but not http imraven Linux - Software 2 03-27-2009 03:20 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
APT on FEDORA 1 giving SEGMENTATION FAULT prester john Linux - Software 3 03-05-2004 05:10 AM
APT on FEDORA 1 giving SEGMENTATION FAULT prester john Linux - Newbie 0 03-03-2004 11:14 PM
APT on FEDORA 1 giving SEGMENTATION FAULT prester john Fedora 0 03-03-2004 11:11 PM


All times are GMT -5. The time now is 08:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration