-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path032-MatrixMult.cpp
48 lines (35 loc) · 929 Bytes
/
032-MatrixMult.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
#include <iostream>
using namespace std;
int main(){
int a,b,c,d;
cout << "Enter order of Matrix 1: ";
cin >> a >>b;
cout << "Enter order of Matrix 2: ";
cin >> c >> d;
if(b!=c){
cout << "Multiplication not possible!";
return 0;
}
int A[a][b], B[c][d];
cout << "Enter elements for Matrix 1: \n";
for(int i=0; i<a; ++i) for(int j=0; j<b; ++j) cin >> A[i][j];
cout << "Enter elements for Matrix 2: \n";
for(int i=0; i<c; ++i) for(int j=0; j<d; ++j) cin >> B[i][j];
int C[a][d];
for (int i = 0; i < a; ++i) {
for (int j = 0; j < d; ++j) {
C[i][j] = 0;
for (int k = 0; k < b; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
for (int i = 0; i < a; ++i) {
cout << "[ \t";
for (int j = 0; j < d; ++j) {
cout << C[i][j] << "\t";
}
cout << " ]" << endl;
}
return 0;
}