-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMatrixTraversal.cpp
More file actions
executable file
·59 lines (49 loc) · 1.09 KB
/
MatrixTraversal.cpp
File metadata and controls
executable file
·59 lines (49 loc) · 1.09 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
#include<iostream>
#define MAXN 100
#define MAXM 100
using namespace std;
int matrix[MAXN][MAXM];
void process(int i, int j) {
cout << matrix[i][j] << " ";
}
void row_major(int n, int m) {
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
process(i, j);
cout << endl << endl;
}
void column_major(int n, int m) {
for(int j=1; j<=m; j++)
for(int i=1; i<=n; i++)
process(i, j);
cout << endl << endl;
}
void snake_order(int n, int m) {
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
process(i, j+(m+1-(2*j))*((i+1)%2));
cout << endl << endl;
}
void diagonal_order(int n, int m) {
int d;
int pcount;
int height;
for(d=1; d<=(m+n-1); d++) {
height = 1 + max(0, d-m);
pcount = min(d, (n-height+1));
for(int j=0; j<pcount; j++)
process(min(m, d)-j, height+j);
}
cout << endl << endl;
}
int main() {
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++)
cin >> matrix[i][j];
}
row_major(3, 3);
column_major(3, 3);
snake_order(3, 3);
diagonal_order(3, 3);
return 0;
}