-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintSpiralArray.cpp
More file actions
64 lines (58 loc) · 1.63 KB
/
PrintSpiralArray.cpp
File metadata and controls
64 lines (58 loc) · 1.63 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
#include <iostream>
using namespace std;
void PrintNum(int n){
cout << n << "\t";
}
void PrintLine(int n, int i){
// Number of integers in current matrix
int n2 = n*n;
// Number of itegers in previous matrix of size n - 1
int m2 = (n - 1)*(n - 1);
if (n % 2 == 0){
if (i == n - 1){
// n is even and we are at the last row so just
// print it
for(int k = n2; k > n2 - n; k--){
PrintNum(k);
}
} else {
// Print row from previous matrix of size n - 1
// first and then print value that belongs to current
// matrix. Previous matrix is at the top left corner
// so no need to adjust row index
PrintLine(n - 1, i);
// Skip all integers from previous matrix and upper
// ones in this columnas integers must form clockwise
// spiral
PrintNum(m2 + 1 + i);
}
} else {
if (i == 0) {
// n is odd and we are at the first row so just
// print it
for(int k = m2 + n; k <= n2; k++) {
PrintNum(k);
}
} else {
// Print value that belongs to current matrix and
// then print row from previous matrix of size n - 1
// Skip all integers from previous matric and bottom
// ones in this column as integers must form clockwise
// spiral
PrintNum(m2 + n - i);
// Previous matrix is at the bottom right corner so
// row index must be reduced by 1
PrintLine(n - 1, i - 1);
}
}
}
void PrintSpiral(int n) {
for(int i = 0; i < n; i++) {
PrintLine(n, i);
cout << endl;
}
}
int main() {
PrintSpiral(5);
return 0;
}