How to do it…

  1. Create two matrices of orders 2 x 3 and 3 x 4 each. 
  2. Before we make the matrix multiplication program, we need to understand how matrix multiplication is performed manually. To do so, let's assume that the two matrices to be multiplied have the following elements:

Figure 1.5
  1. The resultant matrix will be of the order 2 x 4, that is, the resultant matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix:

Figure 1.6

Essentially, the resultant matrix of the order 2 x 4 will have the following elements:

Figure 1.7
  1. The element first row, first column in the resultant matrix is computed using the following formula:

SUM(first element of the first row of the first matrix × first element of the first column of the second matrix), (second element of the first row... × second element of the first column...), (and so on...)

For example, let's assume the elements of the two matrices are as shown in Figure 1.5.  The elements in the first row and the first column of the resultant matrix will be computed as follows:

Figure 1.8
  1. Hence, the element in first row, first column in the resultant matrix will be as follows:
(3×6)+(9×3)+(7×5)
=18 + 27 + 35
=80

Figure 1.9 explains how the rest of the elements are computed in the resultant matrix:

Figure 1.9

The matrixmulti.c program for multiplying the two matrices is as follows:

#include  <stdio.h>
int main()
{
int matA[2][3], matB[3][4], matR[2][4];
int i,j,k;
printf("Enter elements of the first matrix of order 2 x 3 \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matA[i][j]);
}
}
printf("Enter elements of the second matrix of order 3 x 4 \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&matB[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
matR[i][j]=0;
for(k=0;k<3;k++)
{
matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j];
}
}
}
printf("\nFirst Matrix is \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matA[i][j]);
}
printf("\n");
}
printf("\nSecond Matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",matB[i][j]);
}
printf("\n");
}
printf("\nMatrix multiplication is \n");
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",matR[i][j]);
}
printf("\n");
}
return 0;
}

Now, let's go behind the scenes to understand the code better.