-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixmuultiply.cpp
More file actions
87 lines (67 loc) · 1.73 KB
/
matrixmuultiply.cpp
File metadata and controls
87 lines (67 loc) · 1.73 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
vector<vector<int>> multiply(vector<vector<int>> &arr1, vector<vector<int>> &arr2, int r1, int c1, int r2, int c2)
{
// res mat
vector<vector<int>> res(r1, vector<int>(c2, 0));
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
for (int k = 0; k < c1; k++)
{
res[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
return res;
}
int main()
{
int r1, c1, r2, c2;
cout << "num of rows for mat 1: ";
cin >> r1;
cout << "num of cols for mat 1: ";
cin >> c1;
vector<vector<int>> arr1(r1, vector<int>(c1));
cout << " elements of matrix 1: " << endl;
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
cin >> arr1[i][j];
}
}
cout << "number of rows for matrix 2: ";
cin >> r2;
cout << "number of cols for matrix 2: ";
cin >> c2;
// checking agar first ka cols == sec ka rows
if (c1 != r2)
{
cout << "not possible" << endl;
return 0;
}
vector<vector<int>> arr2(r2, vector<int>(c2));
cout << "elements of matrix 2: " << endl;
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
cin >> arr2[i][j];
}
}
vector<vector<int>> result = multiply(arr1, arr2, r1, c1, r2, c2);
cout << "res matrix: " << endl;
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}