-
Notifications
You must be signed in to change notification settings - Fork 0
/
2d_matrix_multiplication.cpp
51 lines (44 loc) · 1.18 KB
/
2d_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
51
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int> > matrixMultiplication(const vector < vector <int> > &matrix1, const vector < vector <int> > &matrix2) {
int a1 = matrix1.size();
int b1 = matrix1[0].size();
int a2 = matrix2.size();
int b2 = matrix2[0].size();
if(b1 != a2) return {};
vector<vector<int> > resultant;
for(int i = 0; i< a1; i++){
vector <int> v;
for(int j = 0; j < b2; j++){
int res = 0;
for(int k = 0; k < b1; k++){
res += matrix1[i][k]*matrix2[k][j];
}
v.push_back(res);
}
resultant.push_back(v);
}
return resultant;
}
int main() {
vector< vector <int> > matrix1 = {
{4, 8},
{0, 2},
{1, 6},
};
vector< vector <int> > matrix2 = {
{5, 2},
{9, 4},
};
vector< vector <int> > resultantMatrix = matrixMultiplication(matrix1, matrix2);
for (int i = 0; i < resultantMatrix.size(); i++)
{
for (int j = 0; j < resultantMatrix[i].size(); j++)
{
cout << resultantMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}