forked from casafurix/c-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixMultiplication.c
More file actions
68 lines (65 loc) · 1.59 KB
/
matrixMultiplication.c
File metadata and controls
68 lines (65 loc) · 1.59 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
62
63
64
65
66
67
68
#include<stdio.h>
#include <stdlib.h>
int main(){
int r1,c1,r2,c2;
//first matrix of r1 x c1, and second of r2 x c2
//taking input for first matrix
printf("Enter r1 and c1: ");
scanf("%d %d",&r1,&c1);
int first[r1][c1];
printf("\nEnter first matrix: \n");
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
scanf("%d",&first[i][j]);
}
}
//input for second matrix
printf("Enter r2 and c2 : ");
scanf("%d %d",&r2,&c2);
int second[r2][c2];
printf("Enter second matrix : \n");
for(int i=0;i<r2;i++){
for(int j=0;j<c2;j++){
scanf("%d",&second[i][j]);
}
}
//printing matrices
printf("First matrix :\n");
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
printf("%d ",first[i][j]);
}
printf("\n");
}
printf("second matrix :\n");
for(int i=0;i<r2;i++){
for(int j=0;j<c2;j++){
printf("%d ",second[i][j]);
}
printf("\n");
}
//check for multiplication
if(c1 != r2){
printf("Matrix not multiplicable \n");
return 0;
}
//multiplication
int result[r1][c2];
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
result[i][j] = 0;
for(int k=0;k<r2;k++){
result[i][j] += first[i][k] * second[k][j];
}
}
}
//printing multiplied matrix
printf("Multiplied Matrix : \n");
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
printf("%d ",result[i][j]);
}
printf("\n");
}
return 0;
}