944,107 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1972
  • C RSS
Oct 14th, 2004
0

Im completely lost on this one

Expand Post »
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 <stdio.h>

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);
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alfy is offline Offline
16 posts
since Oct 2004
Oct 15th, 2004
0

Re: Im completely lost on this one

Try this way:

  1.  
  2. for(i=0;i<2;i++)<blockquote>for(j=0;j<2;j++)<blockquote>for(k=0;k<2;k++)<blockquote>c[i][j] += a[i][k]*b[k][j];
  3. </blockquote></blockquote></blockquote>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Waskar is offline Offline
9 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: error when compiling murphy's law with gcc
Next Thread in C Forum Timeline: I would kill for this, just a little bit





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC