-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplicationofmatrices.c
117 lines (97 loc) · 2.52 KB
/
multiplicationofmatrices.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* multiplication of matrices
let two matrices be A and B
A has order pXq and B has order qXr then on multiplying A and B we get C matrix
C=AxB of order pXr
NOTE: NUMBER OF COLUMNS IN FIRST MATRIX,here 'A'=NUMBER OF ROWS IN SECOND MATRIX,here'B'
author: vedcodes2312 */
#include<stdio.h>
#include<stdlib.h>
int main()
{
system("cls");
int r1,c1,r2,c2;
//for the first matrix
printf("Enter the number of rows in the first matrix:\n");
scanf("%d",&r1);
printf("Enter the number of columns in the first matrix:\n");
scanf("%d",&c1);
//for the second matrix
printf("Enter the number of rows in the second matrix:\n");
scanf("%d",&r2);
printf("Enter the number of columns in the second matrix:\n");
scanf("%d",&c2);
if(c1 != r2)
{
printf("matrices cannot be multiplied");
return 0;
}
int a[r1][c1],b[r2][c2],product[r1][c2];
//first matrix
//entering elements
printf("Enter the elements of the first matrix:\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
printf("element at[%d][%d]",i,j);
scanf("%d",&a[i][j]);
}
}
//printing first matrix
printf("first matrix:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++)
{
printf("[%d][%d]=%d\t ",i,j, a[i][j]);
}
printf("\n");
}
//second matrix
//entering elements
printf("Enter the elements of the second matrix:\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
printf("element at[%d][%d]",i,j);
scanf("%d",&b[i][j]);
}
}
//printing second matrix
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++)
{
printf("[%d][%d]=%d\t ",i,j, b[i][j]);
}
printf("\n");
}
//multiplication of matrices
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
product[i][j]=0;
}
}
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
for(int k=0;k<c1;k++)
{
product[i][j]+=a[i][k]*b[k][j];
}
}
}
//printing the final result
printf("\n result matrix \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("[%d][%d]=%d\t",i,j,product[i][j]);
}
printf("\n");
}
return 0;
}