I need to use array of pointers to multiply the 2 matrices. its working but i m getting the wrong output
.If i give m1=n1=m2=n2 = 2 and both the matrices as 1 2 3 4
. I get the o/p matrix as 7 17 32 54
while the right answer is 7 10 15 22
. PLEASE HELP ME IM STUCK HERE SINCE 2 DAYS...........................................................................
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m1,n1,m2,n2,i,j,k,sum=0,*a[10],*b[10],*c[10];
printf("Enter m1 and n1 : ");
scanf("%d%d",&m1,&n1);
printf("Enter m2 and n2 : ");
scanf("%d%d",&m2,&n2);
if(n1==m2)
{
for(i=0;i<m1;i++)
{
a[i] = (int *)malloc(n1*sizeof(int));
}
for(i=0;i<m2;i++)
{
b[i] = (int *)malloc(n2*sizeof(int));
}
for(i=0;i<m1;i++)
{
c[i] = (int *)malloc(n2*sizeof(int));
}
printf("Enter the first matrix : ");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",(*(a+i)+j));
}
}
printf("Enter the second matrix : ");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",(*(b+i)+j));
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
for(k=0;k<n2;k++)
{
sum += a[i][k] * b[k][j];
}
*(*(c+i)+j) = sum;
}
}
printf("The Resultant Matrix : ");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d ",*(*(c+i)+j));
}
printf("\n");
}
}
else
printf("Invalid Matrix\n");
}