-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpose_matrix.cpp
50 lines (44 loc) · 962 Bytes
/
transpose_matrix.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
#include <iostream>
#include <vector>
void transpose(std::vector<std::vector<int>> &matrix){
int temp{};
for (int i{0}; i < matrix.size(); i++) {
for (int j{0}; j < i; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
int main (int argc, char *argv[])
{
std::vector<std::vector<int>> matrix
{
{1,2,3},
{4,5,6},
{7,8,9},
};
printf("Before transpose");
std::cout<<std::endl;
[=](){
for (int i{0}; i < matrix.size(); ++i) {
for (int j{0}; j < matrix[0].size(); ++j) {
std::cout<<matrix[i][j]<<' ';
}
std::cout<<std::endl;
}
}();
std::cout<<std::endl;
printf("After transpose");
std::cout<<std::endl;
transpose(matrix);
[=](){
for (int i{0}; i < matrix.size(); ++i) {
for (int j{0}; j < matrix[0].size(); ++j) {
std::cout<<matrix[i][j]<<' ';
}
std::cout<<std::endl;
}
}();
return 0;
}