A prerequisite for multiplying two matrices is that the number of columns in the first matrix must be equal to the number of rows in the second matrix.

# Multiplying two matrices

# How to do it…

- Create two matrices of orders
**2 x 3**and**3 x 4**each. - 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:

- 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:

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

- 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:

- 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:

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.

# How it works...

The two matrices are defined `matA` and `matB` of the orders 2 x 3 and 3 x 4, respectively, using the following statement:

int matA[2][3], matB[3][4]

You will be asked to enter the elements of the two matrices using the nested `for` loops. The elements in the matrix are entered in row-major order, in other words, all the elements of the first row are entered first, followed by all the elements of the second row, and so on.

In the nested loops, `for i` and `for j`, the outer loop, `for i`, represents the row and the inner loop, and `for j` represents the column.

While entering the elements of matrices `matA` and `matB`, the values entered in the two matrices will be assigned to the respective index locations of the two-dimensional arrays as follows:

The nested loops that actually compute the matrix multiplication are as follows:

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];

}

}

}

The variable `i` represents the row of the resultant matrix, `j` represents the column of the resultant matrix, and `k` represents the common factor. The common factor

here means the column of the first matrix and the row of the second matrix.

Recall that the prerequisite for matrix multiplication is that the column of the first matrix should have the same number of rows as the second matrix. Because the respective elements have to be added after multiplication, the element has to be initialized to `0` before addition.

The following statement initializes the elements of the resultant matrix:

matR[i][j]=0;

The `for k` loop inside the nested loops helps in selecting the elements in the rows of the first matrix and multiplying them by elements of the column of the second matrix:

matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j];

Let's use GCC to compile the `matrixmulti.c` program as follows:

D:\CBook>gcc matrixmulti.c -o matrixmulti

Let's run the generated executable file, `matrixmulti.exe`, to see the output of the program:

D:\CBook\Chapters\1Arrays>./matrixmultiEnter elements of the first matrix of order 2 x 3397154Enter elements of the second matrix of order 3 x 4

6 2 8 13 9 4 05 3 1 3First Matrix is3 9 71 5 4Second Matrix is6 2 8 13 9 4 05 3 1 3Matrix multiplication is80 108 67 2441 59 32 13

Voilà! We've successfully multiplied two matrices.

# There’s more…

One thing that you might notice while entering the elements of the matrix is that there are two ways of doing it.

- The first method is that you press
*Enter*after inputting each element as follows:

397154

The values will be automatically assigned to the matrix in row-major order, in other words, `3` will be assigned to `matA[0][0]`, `9` will be assigned to `matA[0][1]`, and so on.

- The second method of entering elements in the matrix is as follows:

6 2 8 13 9 4 05 3 1 3

Here, `6` will be assigned to `matB[0][0]`, `2` will be assigned to `matB[0][1]`, and so on.

Now, let's move on to the next recipe!