-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMatrix-multiplication.cpp
More file actions
76 lines (64 loc) · 1.58 KB
/
Matrix-multiplication.cpp
File metadata and controls
76 lines (64 loc) · 1.58 KB
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
#include <iostream>
using namespace std;
void multiply(int[5][5], int[5][5], int, int, int);
int display(int[5][5], int, int);
int main()
{
int a[5][5], b[5][5], r1, c1, r2, c2;
cout << "\n Enter rows for first matrix: ";
cin >> r1;
cout << "\n Enter columns for second matrix: ";
cin >> c1;
cout << "\n Enter rows for first matrix: ";
cin >> r2;
cout << "\n Enter columns for second matrix: ";
cin >> c2;
if (c1 != r2)
return 0;
cout << "\n Enter elements of first matrix \n";
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
cin >> a[i][j];
}
cout << "\n Enter elements of second matrix\n";
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
cin >> b[i][j];
}
display(a, r1, c1);
display(b, r2, c2);
multiply(a, b, r1, c2, c1);
return 0;
}
void multiply(int a[5][5], int b[5][5], int row, int col, int c1)
{
int c[5][5];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
c[i][j] = 0;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
for (int k = 0; k < c1; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
cout << "\n Matrix c after matrix multiplication is:\n";
display(c, row, col);
}
int display(int c[5][5], int row, int col)
{
cout << "\n Matrix is:\n";
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
cout << c[i][j] << " ";
cout << "\n";
}
return 0;
}