Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<stdio.h>
#include<conio.h>
void main()
{
   int a[3][3], b[3][3], c[3][3], i, j, k;
   clrscr();
   printf("Enter the elements of 3*3 matrix a \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     scanf("%d", &a[i][j]);
      }
   }
   printf("Enter the elements of 3*3 matrix b \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
    scanf("%d", &b[i][j]);
      }
   }
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     c[i][j] = 0
     for(k = 0; k < 3; k++)
     {
        c[i][j] = c[i][j] + (a[i][k] * b[k][j])
     }
      }
   }
   printf("The resultant 3*3 matrix c is \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     printf("%d\t", c[i][j]);
      }
      printf("\n");
   }
   getch();
}
Posted
Updated 27-Nov-15 15:58pm
v2
Comments
Patrice T 28-Nov-15 0:04am    
How would you do it ?

1 solution

Because the formula is like that only. So you multiple each item of first row of Matrix A with each item of first column of Matrix B and sum their product to get the total multiplied value.

So a[i][k] will give you first row items of Matrix 1 as k is increasing. So in that inner loop you will multiply a[0][0] * b[0][0]. Next for k=1, it will be a[0][1] * b[1][0]. And so on. So b is increasing by column and a is increasing by row.

You are storing that product in another matrix C.

For formula, refer - Matrix Multiplication[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900