Multiplying two 3x3 Matrix Using User Defined Function and Displaying Result from Main Function


#include<stdio.h>
#include<conio.h>

/* Function Prototype */
void multiply(int a[3][3], int b[3][3], int m[3][3]);

int main()
{
 int a[3][3], b[3][3], m[3][3],i,j;
 clrscr();
 /* Reading First Matrix a */
 printf("Enter First Matrix:\n");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("a[%d][%d]=",i,j);
   scanf("%d", &a[i][j]);
  }
 }
 /* Reading Second Matrix b */
 printf("Enter Second Matrix:\n");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("b[%d][%d]=",i,j);
   scanf("%d", &b[i][j]);
  }
 }
 /* Function Call */
 multiply(a,b,m);
 /* Keep in mind that m is empty array.
    We will multiply a and b matrix in user
    defined function and resultant matrix will
    be stored in m and after function call we can
    access resultant matrix from main function
    since change in array from user defined
    function are automatically reflected back
    in main function */
 /* Displaying Resultant Matrix m */
 printf("Enter First Matrix:\n");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("%d\t", m[i][j]);
  }
  printf("\n");
 }
 getch();
 return 0;
}
/* Function Definition */
void multiply(int a[3][3], int b[3][3], int m[3][3])
{
 int i,j,k;
 /* Multiplication */
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   m[i][j]=0;
   for(k=0;k< 3;k++)
   {
    m[i][j] +=a[i][k] * b[k][j];
   }
  }
 }
}