segmentation error ....!

J

John Doe

segmentation error !!!!
hi guys ,
i wrote this program to multiply two matrices (just the basic code
without checkin 4 the condition n==p )
"
#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("Enter the size of the first array A:");
scanf("%d %d",&m,&n);

printf("Enter the size of the second array A:");
scanf("%d %d",&p,&q);

printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("\nEnter the value:");
scanf("%d",a[j]);
}
printf("\nEnter the value of the Matrix B:\n");

for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\nEnter the value:");
scanf("%d",b[j]);
}

/* MULTIPLICATION */

for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[j]=0;
for(k=0;k<n;k++)
c[j]+=(a[k]*b[k][j]);
}

for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d \t",c[j]);
}
}

"
i compiled this code using gcc compiler.... no errors showed up ...
then i executed ...this is what i got ....
"
Enter the size of the first array A:2
2
Enter the size of the second array A:2
2

Enter the value of the Matrix A:

Enter the value:2
Segmentation fault

"
can u guys please tell me what this segementation error is ..?
and y it was thrown up suddenly in my program ,,?
i've never encountered this error before ...
 
D

Dik T. Winter

> int a[10][10],b[10][10],c[10][10]; ....
> printf("\nEnter the value of the Matrix A:\n");
> for(i=0;i<m;i++)
> for(j=0;j<n;j++)
> {
> printf("\nEnter the value:");
> scanf("%d",a[j]);

This is a strange way to initialise a 2-dimensional array... (Where is
'i' used?) More of this kind of errors do occur.
> for(i=0;i<m;i++)
> for(j=0;j<q;j++)
> {
> c[j]=0;
> for(k=0;k<n;k++)
> c[j]+=(a[k]*b[k][j]);
> }

This is also not understandable. Where is 'i' used?
 
R

Richard Heathfield

John Doe said:
segmentation error !!!!
hi guys ,
i wrote this program to multiply two matrices (just the basic code
without checkin 4 the condition n==p )
"
#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("Enter the size of the first array A:");
scanf("%d %d",&m,&n);

Check that the read succeeded. In this case, you are asking scanf to
populate two objects, so it will return 2 if successful.
printf("Enter the size of the second array A:");
scanf("%d %d",&p,&q);

Check that the read succeeded. In this case, you are asking scanf to
populate two objects, so it will return 2 if successful.

Now would be a good point at which to make sure that none of i, j, m,
and n exceed 10. (At first, I thought this was probably causing your
problem, but then I discovered that your test data comprises 2, 2, 2,
2, so let's move on.)
printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("\nEnter the value:");
scanf("%d",a[j]);

Two points here. Firstly, a[j] is an array, so it evaluates to &a[j][0],
which is fine (it's the address of an int, which is what's required),
but probably not what you want. It's more likely that you want either
&a[j] or perhaps &a[j].

Secondly, check that the read succeeded. In this case, you are asking
scanf to populate one object, so it will return 1 if successful.
}
printf("\nEnter the value of the Matrix B:\n");

for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\nEnter the value:");
scanf("%d",b[j]);

Two points here. Firstly, b[j] is an array, so it evaluates to &b[j][0],
which is fine (it's the address of an int, which is what's required),
but probably not what you want. It's more likely that you want either
&b[j] or perhaps &b[j].

Secondly, check that the read succeeded. In this case, you are asking
scanf to populate one object, so it will return 1 if successful.
}

/* MULTIPLICATION */

for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[j]=0;

See below for a discussion of the above line.

for(k=0;k<n;k++)
c[j]+=(a[k]*b[k][j]);

If either j or k exceeds 9, you're in trouble. And see below.
}

for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d \t",c[j]);

See below.
}
}

"
i compiled this code using gcc compiler.... no errors showed up ...

So did I, and I got:

foo.c:3: warning: return-type defaults to `int'
foo.c:3: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:34: incompatible types in assignment
foo.c:36: invalid operands to binary *
foo.c:43: warning: int format, pointer arg (arg 2)
foo.c:45: warning: control reaches end of non-void function
make: *** [foo.o] Error 1

The first couple are no big deal, although it would be good to address
them:

int main(void)

In my (indent-fixed) version of the source, line 34 is:

c[j] = 0;

And this may well be your blow-up. Here, you're trying to write an int
value to an object that is an array of 10 ints. You can't assign to
arrays in C, only to their members.

In each case, I fixed c[j] to read c[j]. The "invalid operands to
binary *" error was caused by a[k], so I fixed it to read a[j][k]
(check the logic in case that's not what you meant).

The program then runs to completion, but with very strange results. If I
were you, I would define your arrays like this:

int a[10][10] = {0};
int b[10][10] = {0};
int c[10][10] = {0};

This will ensure that they start off with all member values equal to 0.
With that correction, and a puts("") at the end, the output was
reasonable.
"
can u guys please tell me what this segementation error is ..?

It normally means you (in the general sense!) have done something stupid
with arrays or pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top