Matrix Multiplication in C: You can add, deduct, multiply, and divide two matrices (two-dimensional arrays). To do this, we inputs the size (rows and columns) of two matrices using the user’s data. The number of columns of the first matrix must be equal to the rows of the second matrix to multiply two matrices. Then we multiply the entered matrices of the user.
Time Complexity of this algorithm is O(n3).
Matrix multiplication in C language
#include<stdio.h> int main(void) { int b, t, u, w, n, p, l, Result = 0; int first[10][10], second[10][10], multiple[10][10]; printf("Enter the first matrix number of rows and columns \n "); scanf("%d%d", &n, &p); printf(" Enter the elements of first matrix : \n "); for (b = 0; b < n; b++) for (t = 0; t < p; t++) scanf("%d", &first[b][t]); printf("Enter the second matrix number of rows and columns\n"); scanf(" %d %d", &u, &w); if (p != u) printf(" You can not multiply the matrix with each other. \n "); else { printf(" Enter the elements of second matrix \n "); for (b = 0; b < u; b++) for (t = 0; t < w; t++) scanf("%d", &second[b][t] ); for (b = 0; b < n; b++) { for (t = 0; t < w; t++) { for (l = 0; l < u; l++) { Result = Result + first[b][l] * second[l][t]; } multiple[b][t] = Result; Result = 0; } } printf(" The result of the matrix multiplication is : \n "); for (b = 0; b < n; b++) { for (t = 0; t < w; t++) printf("%d \t", multiple[b][t] ); printf(" \n "); } } return 0; }
An output of 3 X 3 matrix multiplication C program:
Explanation
First of all you need to declare certain integer variables b, t, u, w, n , p, l, Result = 0 and second[10], second[10], multiple[10][10], second[10]. Then use a print declaration to instruct the user to enter a number of rows and columns of the first matrix.
Insert all the elements one by one in your loop, followed by a scanf().
for (b = 0; b < n; b++) for (t = 0; t < p; t++) scanf("%d", &first[b][t]);
It is a nested loop that sets values for ‘n’ number of rows and ‘p’ number of columns in the first[] array, i.e., iterates ‘n’ and ‘p’ number times to enter the values. Again, use a separate print statement to notify the user to input the rows and columns of the second matrix.
for (b = 0; b < u; b++) for (t = 0; t < w; t++) scanf("%d", &second[b][t] );
Once again, the same nested loops used to enter the second[] list to the values. It checks whether the number of columns in the first matrix is equal to the rows in the second matrix. If that condition true, the multiplication logic must write. It should be nested in the loop.
for (b = 0; b < n; b++) { for (t = 0; t < w; t++) { for (l = 0; l < u; l++) { Result= Result + first[b][l] * second[l][t]; } multiple[b][t] = Result; Result = 0; } }
The three loops use to store the multiplication value of first[] and the second[] in the Result variable. This addition of multiplicative values will continue until all array values passed through.
printf(" The result of the matrix multiplication is: \n "); for (b = 0; b < n; b++) { for (t = 0; t < w; t++) printf("%d \t", multiple[b][t] ); printf(" \n "); } } return 0; }