-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixMultiplication.c
55 lines (47 loc) · 1.56 KB
/
matrixMultiplication.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
#include <stdio.h>
void main(){
int row1, row2, col1, col2, sum=0;
printf("Enter number of rowns in Matrix 1: ");
scanf("%d", &row1);
printf("Enter number of columns in Matrix 1: ");
scanf("%d", &col1);
printf("Enter number of rowns in Matrix 2: ");
scanf("%d", &row2);
printf("Enter number of columns in Matrix 2: ");
scanf("%d", &col2);
if(col1 != row2){
printf("Matrix cannot be multiplied. To multiply matrix A to B, size of A should be axb and size of B should be bxc.");
return;
}
int mat1[row1][col1], mat2[row2][col2], resultMat[row1][col2];
printf("Enter elements for matrix 1:\n");
for(int i=0; i<row1; i++){
for(int j=0; j<col1; j++){
printf("Enter element at [%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements for matrix 2:\n");
for(int i=0; i<row1; i++){
for(int j=0; j<col1; j++){
printf("Enter element at [%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
}
for(int row = 0; row<row1; row++){
for(int col=0; col<col2; col++){
for(int ele=0; ele<col1; ele++){
sum += mat1[row][ele]*mat2[ele][col];
}
resultMat[row][col] = sum;
sum = 0;
}
}
printf("Result of matrix multiplication is:\n");
for(int i=0; i<row1; i++){
for(int j=0; j<col2; j++){
printf("\t%d\t", resultMat[i][j]);
}
printf("\n");
}
}