LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why is my program giving me segmentation fault? (https://www.linuxquestions.org/questions/programming-9/why-is-my-program-giving-me-segmentation-fault-852320/)

Ahmad Mujtaba 12-25-2010 04:43 AM

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]);
}

imagine_me2 12-25-2010 05:41 AM

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" .

Snark1994 12-25-2010 06:06 AM

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.

johnsfine 12-25-2010 07:06 AM

Quote:

Originally Posted by Ahmad Mujtaba (Post 4202682)
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 (Post 4202701)
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 (Post 4202709)
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?

imagine_me2 12-25-2010 07:16 AM

@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.

johnsfine 12-25-2010 07:33 AM

Quote:

Originally Posted by imagine_me2 (Post 4202739)
@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.


All times are GMT -5. The time now is 12:45 AM.