# Matrix Multiplication in C

## Matrix Multiplication in C Language

Matrix Multiplication in c language: This c program example multiplies two matrices (two-dimensional arrays) and print the output on the screen. In this program user will enter the order of both matrices 1 & 2 whose product is to be calculated. Then user will enter elements of both  matrices.

## Condition for c Matrix Multiplication Program

Note: Matrix multiplication is possible only when the number of columns of first matrix will be equal to number of rows of second matrix.

The order of 1st and 2nd matrices are (r1 * c1) and (r2 * c2) respectively. If c1 is equal to r2 then we will continue with matrix multiplication in c code otherwise an error message “Matrix multiplication in c is not possible” is displayed on the output screen.

The final result matrix will be of the order (r1 * c2).

You should have the knowledge of following C Programming topics to understand Matrix multiplication in c better –

• C array
• C for loop
• C if…else statement

## Matrix Multiplication in C Algorithm

An algorithm is the logic of the program. With Algorithm, it will become easy to understand the program for Matrix Multiplication in c.

• m1[10][10] is 1st matrix
• m2[10][10] is 2nd matrix
• And m3[10][10] is 3rd matrix which stores the result of multiplication of matrices m1 and m2 in c
• r1 & c1 are rows and columns of 1st matrix (m1)
• r2 & c2 are rows and columns of 2nd matrix (m2)

The matrix multiplication program in c is explained as –

1. Input number of rows for and columns for matrix m1 and store in variables r1 & c1
2. Input number of rows for and columns for matrix m2 and store in variables r2 & c2
3. For matrix multiplication, number of columns of 1st matrix should be equal to number of rows of 2nd matrix.
So, if(c1==r2) then continue otherwise exit the loop.
4. Input matrix m1 elements
5. Input matrix m2 elements
6. Then we will run for loop in c to perform matrix multiplication of two matrices m1 and m2, store the result in matrix m3 and also display the result of matrix multiplication in c
7. Initialise matrix m3 with zero ,i.e., m3[10][10] = 0 as bydefault array contain garbage value.
If we not initialise matrix m3 with 0 then the garbage value will also be added during matrix multiplication c program and it will not show correct result.

## Matrix Multiplication C Program

``````
/* Program for Matrix Multiplication in c */

#include <stdio.h> //printf() & scanf() Header File
#include <conio.h> //clrscr() & getch() Header File

int main()
{
int m1[10][10], m2[10][10], m3[10][10], r1, c1, r2, c2, i, j, k;

clrscr(); // to clear output screen

/* To take number of rows & columns for matix m1 from user */
printf("Enter how many rows and cols should be in matrix m1: ");
scanf("%d%d", &r1, &c1);

/* To take number of rows & columns for matix m2 from user */
printf("Enter how many rows and cols should be in matrix m2: ");
scanf("%d%d", &r2, &c2);

/*For matrix multiplication in c columns of 1st matrix should be equal to rows of 2nd matrix*/
if (c1!=r2)
{
printf(Can't multiply as c1 is not equal to r2");
getch();
exit(1);
}

/* Martix 1 Input */
printf(Enter elements for martix m1\n", i+1);
for (i=0; i<r1; i++)
{
for (j=0; j<c1; j++)
{
printf(Enter element %d,%d: ", i+1, j+1);
scanf("%d", &m1[i][j]);
}
}

/* Martix 2 Input */
printf(Enter elements for martix m2\n", i+1);
for (i=0; i<r2; i++)
{
for (j=0; j<c2; j++)
{
printf(Enter element %d,%d: ", i+1, j+1);
scanf("%d", &m2[i][j]);
}
}

/* Martix Multiplication in c code */
for (i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
{
/*initialise m3 matrix with 0 as bydefault array contains garbage value */
m3[i][j] = 0;
for (k=0; k<c1; k++)
{
m3[i][j]+= m1[i][k] * m2[k][j];
}
}
}

/* Martix 3 Output */
printf(Output\n", i+1);
for (i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
{
printf(%d\t", m3[i][j]);
}
printf(\n");
}

getch();
return(0);
}
``````
Output 1 –
``````Enter how many rows and cols should be in matrix m1: 2 3
Enter how many rows and cols should be in matrix m2: 3 2
Enter elements for matrix m1
Enter element 1,1: 2
Enter element 1,2: 3
Enter element 1,3: 1
Enter element 2,1: 4
Enter element 2,2: 6
Enter element 2,3: 9
Enter elements for matrix m2
Enter element 1,1: 1
Enter element 1,2: 6
Enter element 2,1: 2
Enter element 2,2: 7
Enter element 3,1: 3
Enter element 3,2: 11
Output
11    44
43    165
``````
Output 2 –
``````Enter how many rows and cols should be in matrix m1: 2 2
Enter how many rows and cols should be in matrix m2: 3 2
Can't multiply as c1 is not equal to r2
``````

### Matrix Multiplication C Program Explanation

Let the number of rows and columns entered by the user for matrix m1 are 2 and 3 respectively
And let the number of rows and columns entered by the user for matrix m2 are 3 and 2 respectively

1st Iteration    /* Input elements for 1st matrix (m1) */
Enter element 1,1 : 2
Enter element 1,2 : 3
Enter element 1,3 : 1
Enter element 2,1 : 4
Enter element 2,2 : 6
Enter element 2,3 : 9

2nd Iteration    /* Input elements for 2nd matrix (m2) */
Enter element 1,1 : 1
Enter element 1,2 : 6
Enter element 2,1 : 2
Enter element 2,2 : 7
Enter element 3,1 : 3
Enter element 3,2 : 11

3rd Iteration    /* Matrix multiplication in c code*/

1. ```for(i=0; i<2; i++) /* condition is true */
{
for(j=0; j<2; j++) /* condition is true */
{
m3[0][0] = 0;
for (k=0; k<3; k++) /* condition is true */
{
m3[0][0] = m3[0][0] + (m1[0][0] * m2[0][0]);
= 0 + (2 * 1)
= 2
}
}
}
```
2. ```/* Innermost loop */
for (k=1; k<3; k++) /* condition is true */
{
m3[0][0] = m3[0][0] + (m1[0][1] * m2[1][0]);
= 2 + (3 * 2)
= 8
}
}
}
```
3. ```/* Innermost loop */
for (k=2; k<3; k++) /* condition is true */
{
m3[0][0] = m3[0][0] + (m1[0][2] * m2[2][0]);
= 8 + (1 * 3)
= 11
}
}
}
```
4. /* Innermost loop */
for (k=2; k<3; k++) /* condition is false */
So, k loop will not continue
Outer for i loop will continue
5. We have got the first element of m3 matrix ,i.e., m3[0][0] = 11
6. Similarly other elements of matrix m3 will be calculated

4th Iteration    /* Print output on the screen */
11    44
43    165

In this way Matrix multiplication c program will work and output is printed on the screen