-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_multiplication.c
63 lines (58 loc) · 1.26 KB
/
matrix_multiplication.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
#include<stdio.h>
void multiply(int n,int a[n][n],int b[n][n], int ans[n][n]){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
ans[i][j]=0;
for(int k=0;k<n;k++){
ans[i][j]=ans[i][j]+a[i][k]*b[k][j];
}
}
}
}
void main(){
int n;
printf("enter the number of rows in matrix\n");
//we are assuming it's a n x n matrix
scanf("%d",&n);
int a[n][n];
int b[n][n];
printf("enter the first matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int c;
printf("enter a[%d][%d] ",i,j);
scanf("%d",&c);
a[i][j]=c;
}
}
printf("enter the second matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int c;
printf("enter b[%d][%d] ",i,j);
scanf("%d",&c);
b[i][j]=c;
}
}printf("first matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf(" %d ",a[i][j]);
}
printf("\n");
}printf("second matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf(" %d ",b[i][j]);
}
printf("\n");
}
int c[n][n];
multiply(n,a,b,c);
printf("after multiplication we get\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf(" %d ",c[i][j]);
}
printf("\n");
}
}