-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixMult.c
60 lines (56 loc) · 1.64 KB
/
matrixMult.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
void main(){
int dimm1, dimn1, dimm2, dimn2;
printf("Enter dimensions of matrix 1, m x n:\n");
scanf("%d %d", &dimm1, &dimn1);
int mat1[dimm1][dimn1];
for (int i = 0; i < dimm1; i++){
for (int j = 0; j < dimn1; j++){
printf("Enter values for row %d, column %d of matrix 1:\n", (i + 1), (j+1));
scanf("%d", &mat1[i][j]);
}
}
printf("Enter dimensions of matrix 2, m x n:\n");
scanf("%d %d", &dimm2, &dimn2);
int mat2[dimm2][dimn2];
for (int i = 0; i < dimm2; i++){
for (int j = 0; j < dimn2; j++){
printf("Enter values for row %d, column %d of matrix 2:\n", (i + 1), (j+1));
scanf("%d", &mat2[i][j]);
}
}
printf("Matrix 1 is: \n");
for (int i = 0; i < dimm1; i++){
for (int j = 0; j < dimn1; j++){
printf("%4d ", mat1[i][j]);
}
printf("\n");
}
printf("Matrix 2 is: \n");
for (int i = 0; i < dimm2; i++){
for (int j = 0; j < dimn2; j++){
printf("%4d ", mat2[i][j]);
}
printf("\n");
}
int result[dimm1][dimn2];
for (int i = 0; i < dimm1; i++){
for (int j = 0; j < dimn2; j++){
result[i][j] = 0;
}
}
for (int i = 0; i < dimm1; i++){
for (int j = 0; j < dimn2; j++){
for (int k = 0; k < dimn1; k++){
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
printf("Result Matrix is:\n");
for (int i = 0; i < dimm1; i++){
for (int j = 0; j < dimn2; j++){
printf("%3d ", result[i][j]);
}
printf("\n");
}
}