-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwave.cpp
More file actions
57 lines (49 loc) · 1.32 KB
/
Copy pathwave.cpp
File metadata and controls
57 lines (49 loc) · 1.32 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
#include <iostream>
using namespace std;
void wavePrint(int input[][1001], int nRows, int mCols);
void print2dArray(int arr[][1001], int m, int n);
int main() {
int input[][1001] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
wavePrint(input, 3, 4);
return 0;
}
void wavePrint(int input[][1001], int nRows, int mCols)
{
// 0 0 | 0 1 | 0 2 | 1 2 | 1 1 | 1 0 | 2 0 | 2 1 | 2 2
int row = 0;
int col = 0;
int i = 0;
while (i != (nRows * mCols)) {
bool goingReverse = (col % 2 == 0);
// cout << "[i] row: " << row << " col: " << col << " goingReverse: " << goingReverse << endl;
if (row == nRows || row < 0) {
if (goingReverse) {
row = nRows - 1;
} else {
row = 0;
}
goingReverse = !goingReverse;
col++;
}
// cout << "[f] row: " << row << " col: " << col << " goingReverse: " << goingReverse << endl;
cout << input[row][col] << " ";
if (goingReverse) {
row++;
} else {
row--;
}
i++;
}
}
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;
}
}