954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Im completely lost on this one

i am suppose to multiply the two matrices together using a for loop, but the only way i can figure it out is by using printf( a[0][0] * b[0][0] + a[0][1] * b[1][0]) 4 times to find each piece. Please help.


#include

int main()
{
int i,j,k;
int a[2][2]={{1,2},{3,2}};
int b[2][2]={{3,4},{2,1}};
int c[2][2];
printf("Matrix A:\n");
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Matrix B:\n");
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
printf("AxB=Matrix C:\n");

printf("%d ", a[0][0] * b[0][0] + a[0][1] * b[1][0]);
printf("%d \n", a[0][0] * b[0][1] + a[0][1] * b[1][1]);
printf("%d ", a[1][0] * b[0][0] + a[1][1] * b[1][0]);
printf("%d ", a[1][0] * b[0][1] + a[1][1] * b[1][1]);
/* for (j=0; j<1; j++)
{
for (i=0; i<1; i++)
{
c[i][j] = (a[i][j] * b[i][j]) + (a[i][j] * b[i][j]);
printf("%d ", c[i][j]);
}
printf("\n");
}
I couldnt figure out how to set this in a loop to solve for matrixC
*/
system("PAUSE");
return(0);
}

Alfy
Newbie Poster
16 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Try this way:

for(i=0;i<2;i++)[indent]for(j=0;j<2;j++)[indent]for(k=0;k<2;k++)[indent]c[i][j] += a[i][k]*b[k][j];
[/indent][/indent][/indent]
Waskar
Newbie Poster
9 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You