forked from AllAlgorithms/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
54 lines (51 loc) · 984 Bytes
/
matrix.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
#include<stdio.h>
#define MAX 2
int m =2;
int n =2;
int p =2;
void MatrixMutiply(int m,int n,int p,int matrix1[MAX][MAX],
int matrix2[MAX][MAX],int matrixResult[MAX][MAX])
{
int sum;
for(int i=0;i<m;i++)
for(int j=0;j<p;j++)
{
sum=0;
for(int k=0;k<n;k++)
sum+=matrix1[i][k]*matrix2[k][j];
matrixResult[i][j]=sum;
}
}
int main()
{
int matrix1[MAX][MAX] ={0};
int matrix2[MAX][MAX] = {0};
int matrixResult[MAX][MAX] = {0};
int temp=0;
int m =2;
int n =2;
int p =2;
printf("\nPlease elements of Matrix1(%d*%d):\n",m,n);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
scanf("%d",&temp);
matrix1[i][j]=temp;
}
printf("\nPlease elements of Matrix2(%d*%d):\n",n,p);
for(int i=0;i<n;i++)
for(int j=0;j<p;j++)
{
scanf("%d",&temp);
matrix2[i][j]=temp;
}
MatrixMutiply(m,n,p,matrix1,matrix2,matrixResult);
printf("\nResult matrix: \n");
for(int i=0;i<m;i++)
{
for(int j=0;j<p;j++)
printf("%d ",matrixResult[i][j]);
printf("\n");
}
return 0;
}