-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmatrixprod.c
53 lines (44 loc) · 1.25 KB
/
matrixprod.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
#include<stdio.h>
int main(){
int r1,c1,r2,c2;
printf("Enter the number of rows and columns for matrix 1: ");
scanf("%d %d", &r1,&c1);
printf("Enter the number of rows and columns for matrix 2: ");
scanf("%d %d", &r2, &c2);
if(c1!=r2){
printf("Column of the first matrix is not equal to the second matrix\n");
return 0;
}
//Entering first matrix elements
int a[r1][c1], b[r2][c2], res[r1][c2];
printf("Enter the elements of the first matrix\n");
for(int i=0; i<r1; i++){
for(int j=0; j<c1; j++){
scanf("%d", &a[i][j]);
}
}
//Entering second matrix elements
printf("Enter the elements of the second matrix\n");
for(int i=0; i<r2; i++){
for(int j=0; j<c2; j++){
scanf("%d", &b[i][j]);
}
}
//Multiplying them
for(int i=0; i<r1; i++){
for(int j=0; j<c2; j++){
res[i][j]=0;
for(int k=0; k<c1; k++){
res[i][j] += a[i][k]*b[k][j];
}
}
}
//Printing the result matrix
printf("The Result matrix is:\n");
for(int i=0; i<r1; i++){
for(int j=0; j<c2; j++)
printf("%d ", res[i][j]);
printf("\n");
}
return 0;
}