forked from vishnu2k60/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix Multiplication .c
77 lines (65 loc) · 1.13 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
64
65
66
67
68
69
70
71
72
73
74
75
76
//C PROGRAM FOR MATRIX MULTIPLICATION
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct MAT
{ int n_row,n_col;
int A[MAX][MAX];
};
typedef struct MAT MATRIX;
void input(MATRIX*);
void display(MATRIX);
MATRIX multiply(MATRIX,MATRIX);
int main()
{
MATRIX M1, M2, R, T;
input(&M1);
input(&M2);
T=multiply(M1,M2);
display(T);
return 0;
}
void input (MATRIX *p)
{
int i, j;
printf("\n Enter the no. of rows:");
scanf("%d", &p->n_row);
printf("\n Enter the no. of columns:");
scanf("%d", &p->n_col);
for(i=0;i<p->n_row; i++)
for(j=0;j<p->n_col; j++)
{
printf("\n Enter Element [%d][%d]:",i+1,j+1);
scanf("%d", &p->A[i][j]);
}
}
void display (MATRIX M )
{
int i, j;
for(i=0;i<M.n_row; i++)
{
printf("\n ");
for(j=0;j<M.n_col; j++)
printf( "%d ",M.A[i][j]);
}
}
MATRIX multiply(MATRIX M1,MATRIX M2)
{
MATRIX T;
int i, j, k;
if(M1.n_col!=M2.n_row)
{
printf("\n Matrix multiplication is impossible…");
exit(1);
}
T.n_row=M1.n_row;
T.n_col=M2.n_col;
for(i=0;i<T.n_row; i++)
for(k=0;k<T.n_col; k++)
{
T.A[i][k]=0;
for(j=0; j<M1.n_col; j++)
T.A[i][k]+=M1.A[i][j]*M2.A[j][k];
}
return T;
}