help wid c program 2 multiply 2 matrices....

Status
Not open for further replies.

QwertyManiac

Commander in Chief
You missed using Ampersands while taking array inputs.

The below program is right and executes well for two 3x3 matrices filled with '3'. The output being a matrix with every element as 3 cubed (27).

Code:
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],r[3][3],i,j,c,d,s;
i=0;
j=0;
c=0;
d=0;
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("enter 1 :");
scanf("%d",&a[i][j]);
}
}
i=0;
j=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{

printf("enter 2 :");
scanf("%d",&b[i][j]);
}
}
s=0;
for(c=0;c<3;c++)
{
for(d=0;d<3;d++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
s=s+(a[i][j]*b[j][i]);
}
r[c][d]=s;
s=0;
}
}

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

}

I haven't altered any logic, though I guess you sure can shorten the code a lot.

Check this thread too.
 
Status
Not open for further replies.
Top Bottom