-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix_problem.cpp
More file actions
64 lines (52 loc) · 1.56 KB
/
matrix_problem.cpp
File metadata and controls
64 lines (52 loc) · 1.56 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
//
// Created by Mayank Parasar on 2020-03-09.
//
/*Simple kernel to add matrix*/
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector<vector<int>> mat = {{1, 2, 3, 4, 5},
{2, 3, 4, 5, 6},
{3, 4, 5, 6, 7},
{4, 5, 6, 7, 8}};
for(int ii =0; ii < mat.size(); ii++) { // loop running through rows
for(int jj = 0; jj < mat[ii].size(); jj++) { // loop running through cols
cout << mat[ii][jj] << " ";
}
cout << endl;
}
// sum the diagonal
int sum = 0;
for(int ii =0; ii < mat.size(); ii++) { // loop running through rows
for(int jj = 0; jj < mat[ii].size(); jj++) { // loop running through cols
if(ii == jj)
sum += mat[ii][jj];
}
// cout << endl;
}
cout << "sum: " << sum << endl;
// transpose
vector<vector<int>> mat_tr;
int rows = mat.size();
int cols = mat[0].size();
// get the stride.
// for(int rw)
vector<int> row;
for(int c = 0; c < cols; c++) {
for(int r = 0; r < rows; r++) {
// cout << mat[r][c] << " ";
row.push_back(mat[r][c]);
}
mat_tr.push_back(row);
row.clear();
cout << endl;
}
for(int ii =0; ii < mat_tr.size(); ii++) { // loop running through rows
for(int jj = 0; jj < mat_tr[ii].size(); jj++) { // loop running through cols
cout << mat[ii][jj] << " ";
}
cout << endl;
}
return 0;
}