-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplier.c
More file actions
61 lines (47 loc) · 1.16 KB
/
Copy pathMatrixMultiplier.c
File metadata and controls
61 lines (47 loc) · 1.16 KB
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
61
#include <stdio.h>
int main(){
//Taking the matrices input
int r1 = 0, c1 = 0, r2 = 0, c2 = 0;
printf("Input the dimensions of the first matrix: ");
scanf("%d%d", &r1, &c1);
int a[r1][c1];
printf("\nInput the matrix:\n");
for(int j = 0; j < r1; j++){
for(int k = 0; k < c1; k++){
scanf("%d", &a[j][k]);
}
}
printf("Input the dimensions of the second matrix: ");
scanf("%d%d", &r2, &c2);
int b[r2][c2];
printf("\nInput the matrix:\n");
for(int j = 0; j < r2; j++){
for(int k = 0; k < c2; k++){
scanf("%d", &b[j][k]);
}
}
//Multiplying the matrices
if(r2 != c1){
printf("\nCannot multiply the matrices.\n");
return 0;
}
int c[r1][c2];
for(int j = 0; j < r1; j++){
for(int k = 0; k < c2; k++){
int sum = 0;
for(int l = 0; l < c1; l++){
sum += a[j][l] * b[l][k];
}
c[j][k] = sum;
}
}
//Printing the output
printf("\nThe result is:\n");
for(int j = 0; j < r1; j++){
for(int k = 0; k < c2; k++){
printf("%d ", c[j][k]);
}
printf("\n");
}
return 0;
}