-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
60 lines (51 loc) · 1.48 KB
/
main.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>
#include <stdlib.h>
int main(int argc, char** argv) {
setbuf(stdout, NULL);
int aMatrixHeight, aMatrixWidth, bMatrixHeight, bMatrixWidth;
//Input matrices sizes
printf("Matrix 1 (Rows Columns) - ");
scanf("%d %d",&aMatrixHeight,&aMatrixWidth);
printf("Matrix 2 (Rows Columns) - ");
scanf("%d %d",&bMatrixHeight,&bMatrixWidth);
if(aMatrixWidth == bMatrixHeight){
printf("The result will have\n%d rows e %d columns",aMatrixHeight,bMatrixWidth);
//Input matrices
int aMatrix[aMatrixHeight][aMatrixWidth], bMatrix[bMatrixHeight][bMatrixWidth];
printf("Matrix 1\n");
for(int i = 0; i < aMatrixHeight; i++){
for(int j = 0; j < aMatrixWidth; j++){
printf("Row %d, Column %d - ",i+1,j+1);
scanf("%d",&aMatrix[i][j]);
}
}
printf("Matrix 2\n");
for(int i = 0; i < bMatrixHeight; i++){
for(int j = 0; j < bMatrixWidth; j++){
printf("Row %d, Column %d - ",i+1,j+1);
scanf("%d",&bMatrix[i][j]);
}
}
//Crunch numbers
int resultMatrix[aMatrixHeight][bMatrixWidth];
for(int i = 0; i < aMatrixHeight; i++){
for(int j = 0; j < bMatrixWidth; j++){
int cellResult = 0;
for(int k = 0; k < aMatrixWidth; k++){
cellResult += aMatrix[i][k] * bMatrix[k][j];
}
resultMatrix[i][j] = cellResult;
}
}
//Print final table
for(int i = 0;i < aMatrixHeight;i++){
for(int j = 0; j < bMatrixWidth;j++){
printf("%d\t",resultMatrix[i][j]);
}
printf("\n");
}
}else{
printf("Can't use this sizes");
}
return 0;
}