C Program to Multiply Two Matrices using Multi-dimensional Arrays
C program to Multiply two Matrices using Multi-dimensional Arrays.
In this article, you will learn how to multiply the two matrics using the multi-dimensional arrays.
Example
Enter No. of Row & Column:
2
3
Enter the values of the first matrix - [2 x 3]
Enter value - M1[0][0]: 1
Enter value - M1[0][1]: 2
Enter value - M1[0][2]: 3
Enter value - M1[1][0]: 4
Enter value - M1[1][1]: 5
Enter value - M1[1][2]: 6
Enter the values of the second matrix - [3 x 2]
Enter value - M2[0][0]: 0
Enter value - M2[0][1]: 9
Enter value - M2[1][0]: 8
Enter value - M2[1][1]: 7
Enter value - M2[2][0]: 6
Enter value - M2[2][1]: 5
Multiplication of two matrices matrix_1 & matrix_2
34 38
76 101
You should have knowledge of the following topics in c programming to understand this program:
- C For Loop
- C Arrays
Source
// C program to Multiply two Matrices using Multi-dimensional Arrays
#include <stdio.h>
// It's the main function of the program
int main() {
int rows_1, col_1;
printf("Enter No. of Row & Column:\n");
scanf("%d %d", &rows_1, &col_1);
// Transpose the dimension of the first matrix into the second matrix
// M1[2 x 3] can multiply with M2[3 x 2]
int rows_2 = col_1, col_2 = rows_1;
int matrix_1[rows_1][col_1];
int matrix_2[rows_2][col_2];
int matrix_mul[rows_1][rows_1];
// Step-1 Enter the values of first matrix
printf("\nEnter the values of the first matrix - [%d x %d]\n", rows_1, col_1);
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < col_1; j++) {
printf("Enter value - M1[%d][%d]: ", i, j);
scanf("%d", &matrix_1[i][j]);
}
}
// Step-2 Enter the values of second matrix
printf("\nEnter the values of the second matrix - [%d x %d]\n", rows_2, col_2);
for (int i = 0; i < rows_2; i++) {
for (int j = 0; j < col_2; j++) {
printf("Enter value - M2[%d][%d]: ", i, j);
scanf("%d", &matrix_2[i][j]);
}
}
// Step-3 Initialize the multiplication matrix with 0 values
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < rows_2; j++) {
matrix_mul[i][j] = 0;
}
}
// Step-4 Multiply the matrices M1 & M2 and store these values in another matrices `matrix_mul`
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < col_2; j++) {
for (int k = 0; k < col_1; k++) {
matrix_mul[i][j] += matrix_1[i][k] * matrix_2[k][j];
}
}
}
// Step-5 Final output of the program to print the final matrices
printf("\nMultiplication of two matrices matrix_1 & matrix_2\n");
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < col_2; j++) {
printf("%d\t", matrix_mul[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter No. of Row & Column:
3
3
Enter the values of the first matrix - [3 x 3]
Enter value - M1[0][0]: 2
Enter value - M1[0][1]: 3
Enter value - M1[0][2]: 4
Enter value - M1[1][0]: 3
Enter value - M1[1][1]: 5
Enter value - M1[1][2]: 6
Enter value - M1[2][0]: 4
Enter value - M1[2][1]: 5
Enter value - M1[2][2]: 3
Enter the values of the second matrix - [3 x 3]
Enter value - M2[0][0]: 1
Enter value - M2[0][1]: 2
Enter value - M2[0][2]: 1
Enter value - M2[1][0]: -1
Enter value - M2[1][1]: 2
Enter value - M2[1][2]: 1
Enter value - M2[2][0]: 3
Enter value - M2[2][1]: 2
Enter value - M2[2][2]: 1
Multiplication of two matrices matrix_1 & matrix_2
11 18 9
16 28 14
8 24 12