-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterativeMatrix_Multiplication.cpp
77 lines (68 loc) · 1.91 KB
/
iterativeMatrix_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
using namespace std;
#define MAXn 4
#define MAXvalue 10
vector<vector<int>> generateMatrix(int n){
vector<vector<int>> v(n,vector<int>(n,0));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
v[i][j]=rand()%MAXvalue;
}
}
return v;
}
void displayMatrix(vector<vector<int>> a){
for(int i=0;i<a.size();i++){
for(int j=0;j<a[i].size();j++){
cout<<a[i][j]<<"\t";
}
cout <<endl;
}
cout <<endl;
}
vector<vector<int>> iterativeMultiplication(vector<vector<int>> a,vector<vector<int>> b){
int r = a.size();
int c = b[0].size();
vector<vector<int>> v(r,vector<int>(c,0));
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
v[i][j] = 0;
for(int k=0;k<a[0].size();k++){
v[i][j] += a[i][k]*b[k][j];
}
}
}
return v;
}
vector<vector<int>> MSum(vector<vector<int>> a,vector<vector<int>> b){
vector<vector<int>> c(a.size());
for(int i=0;i<a.size();i++){
for(int j=0;j<a[0].size();j++){
c[i].push_back(a[i][j]+b[i][j]);
}
}
return c;
}
int main(){
vector<vector<int>> a;
vector<vector<int>> b;
vector<vector<int>> c;
cout<<"Enter the number of test cases: ";
int t; cin>>t;
int i,n;
for(i=1;i<=t ;i++){
cout<<"-------------------------------------- TEST CASE "<<i<<" --------------------------------------"<<endl;
n = (rand()%MAXn )+1;
cout << "n = "<<n<<endl;
a = generateMatrix(n);
b = generateMatrix(n);
cout<<"Matrix A"<<endl;
displayMatrix(a);
cout<<"Matrix B"<<endl;
displayMatrix(b);
cout<<"Matrix C = A*B using itereative Multiplication"<<endl;
displayMatrix(iterativeMultiplication(a,b));
}
return 0;
}