-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixmultiply.c
72 lines (62 loc) · 1.52 KB
/
matrixmultiply.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
61
62
63
64
65
66
67
68
69
70
71
72
#include<stdio.h>
#define MAX 50
void main(){
int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
int arows, acolumns, brows, bcolumns;
int i,j,k;
printf("Enter number of rows and columns of matrix a -\n");
scanf("%d %d", &arows, &acolumns);
printf ("Input Matrix A \n");
for(i=0; i<arows; i++){
for(j=0; j<acolumns; j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter number of rows and columns of matrix b -\n");
scanf("%d %d", &brows, &bcolumns);
printf ("Input Matrix B \n");
for(i=0; i<brows; i++){
for(j=0; j<bcolumns; j++){
scanf("%d",&b[i][j]);
}
}
// print matrix A
for(i=0; i<arows; i++){
for(j=0; j<acolumns;j++){
printf("%d",a[i][j]);
}
printf("\n");
}
// print matrix B
for(i=0; i<brows; i++){
for(j=0; j<bcolumns;j++){
printf("%d",b[i][j]);
}
printf("\n");
}
//check condition for matrix multiplication
if(acolumns != brows){
printf("Multiplication not possible :(");
}
else{
// multiply code
int sum = 0;
for(i=0;i<arows;i++){
for(j=0;j<bcolumns;j++){
for (k=0; k<brows; k++){
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
sum=0;
}
}
// print matrix c
printf("Mtrix C is \n");
for(i=0; i<arows; i++){
for(j=0; j<bcolumns;j++){
printf("%d ",c[i][j]);
}
printf("\n");
}
}
}