-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixMultiplication.cpp
60 lines (58 loc) · 1.33 KB
/
MatrixMultiplication.cpp
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
// MatrixMultiplication.cpp : Program to multiply two matrices
#include <iostream>
using namespace std;
int main()
{
int A[2][3], B[2][3], C[2][3], row1, column1, row2, column2;
cout << "Enter no of rows and columns for first matrix: " << endl;
cin >> row1 >> column1;
cout << "Enter no of rows and columns for second matrix: " << endl;
cin >> row2 >> column2;
while (column1!=row2)
{
cout << "Enter no of rows and columns for first matrix again: " << endl;
cin >> row1 >> column1;
cout << "Enter no of rows and columns for second matrix again: " << endl;
cin >> row2 >> column2;
}
cout << "Enter elements of first matrix: " << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column1 ; j++)
{
cin >> A[i][j];
}
}
cout << "Enter elements of second matrix: " << endl;
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < column2; j++)
{
cin >> B[i][j];
}
}
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column2; j++)
{
C[i][j] = 0;
for (int k = 0; k < column1; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Product of two matrices: " << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column2; j++)
{
cout << C[i][j]<<" ";
if (j == column2 - 1)
{
cout << endl;
}
}
}
return 0;
}