-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.cpp
More file actions
67 lines (62 loc) · 1.78 KB
/
Copy pathMatrixMultiplication.cpp
File metadata and controls
67 lines (62 loc) · 1.78 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
#include <iostream>
using namespace std;
int main(){
int r1,r2,c1,c2,i,j,k;
cout << "Enter 1st matrix row and column number: ";
cin >> r1 >> c1;
cout << "Enter 2nd matrix row and column number: ";
cin >> r2 >> c2;
if(c1 != r2) {
cout << "Matrix dimension do not match for multiplication" << endl;
return 1;
}
// 1st matrix column number = 2nd matrix row number then it do multiply (c1=r2)
int array1[r1][c1], array2[r2][c2], result[r1][c2];
// Initialize the result matrix to zero
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
result[i][j] = 0;
}
}
cout << "Enter matrix 1: ";
for(i = 0; i<r1; i++){
for(j = 0; j<c1; j++){
cin >> array1[i][j];
}
}
cout << "Enter matrix 2: ";
for(i = 0; i<r2; i++){
for(j = 0; j<c2; j++){
cin >> array2[i][j];
}
}
cout << "Print Matrix 1: " << endl;
for(i = 0; i<r1; i++){
for(j = 0; j<c1; j++){
cout << array1[i][j] << " ";
}
cout << endl;
}
cout << endl << "Print Matrix 2: " << endl;
for(i = 0; i<r2; i++){
for(j = 0; j<c2; j++){
cout << array2[i][j] << " ";
}
cout << endl;
}
for(i = 0; i<r1; i++){
for(j = 0; j<c2; j++){
for(k = 0; k<c1; k++){
result[i][j] += array1[i][k] * array2[k][j];
}
}
}
cout << "Matrix Multiplication Result: " << endl;
for(i = 0; i<r1; i++){
for(j = 0; j<c2; j++){
cout << result[i][j] << " ";// Result matrix = 1st matrix row and 2nd matrix column (r1=c2)
}
cout << endl;
}
return 0;
}