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);     
}

Edited 3 Years Ago by Dani: Formatting fixed

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]
This article has been dead for over six months. Start a new discussion instead.