-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranspose.cpp
More file actions
51 lines (45 loc) · 1.2 KB
/
transpose.cpp
File metadata and controls
51 lines (45 loc) · 1.2 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
#include <iostream>
using namespace std;
void print2dArray(int arr[][1001], int m, int n);
void transpose(int row, int col, int input[][1001]);
int main() {
int n;
int m;
cin >> n >> m;
int arr[10][1001];
int anc[100];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
cout << endl;
transpose(n, m, arr);
return 0;
}
void transpose(int row, int col, int input[][1001]) {
int maxIndex = row;
if (col > maxIndex) {
maxIndex = col;
}
for (int i = 0; i < maxIndex; i++) {
// cout << "i:: " << i << endl;
for (int j = i; j < maxIndex; j++) {
// cout << i << " " << j << endl;
int temp = input[i][j];
// cout << "i: " << i << " j: " << j << " switching " << temp << " with " << input[j][i] << endl;
input[i][j] = input[j][i];
input[j][i] = temp;
}
}
cout << endl << endl;
print2dArray(input, col, row);
}
void print2dArray(int arr[][1001], int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}