forked from rahul22mrk/hackoctoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_Multiplication.cpp
50 lines (46 loc) · 1.06 KB
/
Matrix_Multiplication.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
#include <bits/stdc++.h>
using namespace std;
void exit(){
cout<<"Matrices cannot be multiplied!";
}
int main(){
int a01, b01, a02, b02;
cin>>a01>>b01>>a02>>b02;
int a1[a01][b01];
int a2[a02][b02];
for(int i=0;i<a01;i++){
for(int j=0;j<b01;j++){
cin>>a1[i][j];
}
}
for(int i=0;i<a02;i++){
for(int j=0;j<b02;j++){
cin>>a2[i][j];
}
}
if(b01 != a02){
exit();
}
else{
int a3[a01][b02];
for(int i=0;i<a01;i++){
for(int j=0;j<b02;j++){
a3[i][j]=0;
for(int k=0;k<b02;k++){
if(a1[i][k]%2==0 && a2[k][j]%2==0){
a3[i][j]+= a1[i][k]*a2[k][j];
}
else{
a3[i][j]+= a1[i][k];
}
}
}
}
for(int i=0;i<a01;i++){
for(int j=0;j<b02;j++){
cout<<a3[i][j]<<" ";
}
cout<<endl;
}
}
}