-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatrix_multiplication.cpp
More file actions
59 lines (49 loc) · 1.43 KB
/
matrix_multiplication.cpp
File metadata and controls
59 lines (49 loc) · 1.43 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
#include<iostream>
using namespace std;
int main(){
int m,n,p,q;
cout << "Enter row & coloumn number of first matrix: ";
cin >> m >> n;
int a[m][n];
cout << "Enter row & coloumn number of second matrix: ";
cin >> p >> q;
int b[p][q];
//in order to multiply two matrices coloumn no. of first matrix must be same with row no. of second matrix
int result[m][q];
if(n != p){
cout<<"Matrices cannot be multiplied together"<<endl;
}
else{
cout<<"Enter the first matrix: "<<endl;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
cin>>a[i][j];
}
}
cout<<"Enter the second matrix: "<<endl;
for(int i=0; i<p; i++){
for(int j=0; j<q; j++){
cin>>b[i][j];
}
}
for(int i=0; i<m; i++){ //as the resultant matrix would be of m x q order
for(int j=0; j<q; j++){
int sum = 0;
//calculate result
for(int k=0; k<n; k++){ //or k<p
sum += a[i][k] * b[k][j];
}
result[i][j]=sum;
}
}
//print the result
cout<<"The resultant matrix is:"<<endl;
for(int i=0; i<m; i++){
for(int j=0; j<q; j++){
cout<<result[i][j];
cout<<"\t";
}
cout<<endl;
}
}
}