Here we are going to solve multiplication of two matrices using C Program.
Multiplication of two Matrices :
ArrayMultiplication.c
#include
#define ROW1 3
#define COL1 4
#define ROW2 COL1
#define COL2 3
int main(void) {
int i, j, k;
int matx1[ROW1][COL1], matx2[ROW2][COL2], matx3[ROW1][COL2];
printf("Enter Matrix 1 (%dx%d) :\n", ROW1, COL1);
for (i = 0; i < ROW1; i++) {
for (j = 0; j < COL1; j++) {
scanf("%d", &matx1[i][j]);
}
}
printf("Enter Matrix 2 (%dx%d) :\n", ROW2, COL2);
for (i = 0; i < ROW2; i++) {
for (j = 0; j < COL2; j++) {
scanf("%d", &matx2[i][j]);
}
}
/*Multiplication Logic Here*/
for (i = 0; i < ROW1; i++) {
for (j = 0; j < COL2; j++) {
matx3[i][j] = 0;
for (k = 0; k < COL1; k++){
matx3[i][j] += matx1[i][k] * matx2[k][j];
}
}
}
printf("The Resultant Matrix is :\n");
for (i = 0; i < ROW1; i++) {
for (j = 0; j < COL2; j++)
printf("%5d", matx3[i][j]);
printf("\n");
}
return 0;
}
Output:
Terminal
Enter Matrix 1 (3x4) :
1 2 3 4
7 8 9 4
9 8 7 5
Enter Matrix 2 (4x3) :
4 5 6
7 8 9
1 2 3
3 6 9
The Resultant Matrix is :
33 51 69
105 141 177
114 153 192
Happy Learning 🙂