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[i][j] or perhaps &a[j][i].
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[i][j] or perhaps &b[j][i].
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[i][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.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.