-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsquareNumbers.cpp
161 lines (128 loc) · 3.72 KB
/
squareNumbers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* -----------------------------------------------------------------
* author : Richard Tenorio
* date : 2020-10-15
* description : Read the README.md file
* compiler : gcc version 10.2.0 (Homebrew GCC 10.2.0)
* flags : -DCMAKE_CXX_FLAGS="-Wall -Wextra -Wconversion
* -Wsign-conversion -pedantic"
* -----------------------------------------------------------------
*/
#include <iostream> // cout, cin etc..
#include <cstdlib> // EXIT_SUCCESS
#include <iomanip> // setw, setfill
#include <cmath> // log10
using namespace std;
int row(int n, int nbDigits);
int column(int n, int nbDigits);
int spiral(int n, int nbDigits);
void turn(int& dx, int& dy);
int main(int argc, char** argv) {
if (argc >= 1 && argv[1] != NULL){
int n;
try {
n = stoi(argv[1]);
} catch(invalid_argument& e){
cout << "you must pass an integer as argument";
return EXIT_FAILURE;
}catch(out_of_range& e){
cout << "the integer passed was too long";
return EXIT_FAILURE;
}
int nbDigits = (int)log10((n*n))+1; //get number of digits n squared
row(n,nbDigits);
column(n, nbDigits);
spiral(n, nbDigits);
} else {
cout << "You must pass an integer as argument to run this program"
<< endl;
}
return EXIT_SUCCESS;
}
int row(int n, int nbDigits){
cout << "row: " << n << " * " << n << endl;
int squared = n * n;
for (int i = 1; i <= squared; ++i){
cout << setw(nbDigits) << setfill('0') << i << " ";
if(i % n == 0){
cout << endl;
}
}
cout << endl;
return EXIT_SUCCESS;
}
int column(int n, int nbDigits){
cout << "column: " << n << " * " << n << endl;
int counter = n;
for(int i = 1; i <= n; ++i){
cout << setw(nbDigits) << setfill('0') << i << " ";
int curr = i;
for(int j = 1; j < counter; ++j){
curr += n;
cout << setw(nbDigits) << setfill('0') << curr << " ";
}
cout << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
int spiral(int n, int nbDigits){
int square = n * n;
int matrice[n][n];
cout << "spiral: " << n << " * " << n << endl;
// create matrice
for(int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
matrice[i][j] = 0;
}
}
int x = n - 1;
int y = 0;
int dx = -1;
int dy = 0;
int new_x;
int new_y;
for(int current = square; current > 0; --current){
matrice[y][x] = current;
new_x = x + dx;
new_y = y + dy;
if(new_x >= 0
&& new_x < n
&& new_y >= 0
&& new_y < n
&& !matrice[new_y][new_x]){
x = new_x;
y = new_y;
} else {
turn(dx, dy);
x = x + dx;
y = y + dy;
}
}
// print matrice
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
cout << setw(nbDigits) << setfill('0') << matrice[i][j] << " ";
}
cout << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
void turn(int& dx, int& dy){
if (dx == 0 && dy == -1) { // UP
dx = -1; // LEFT
dy = 0;
} else if (dx == 1 && dy == 0) { // RIGHT
dx = 0; // UP
dy = -1;
} else if (dx == 0 && dy == 1) { // DOWN
dx = 1; // RIGHT
dy = 0;
} else if(dx == -1 && dy == 0) { // LEFT
dx = 0; // DOWN
dy = 1;
} else {
cout << "Error: Couldn't chose where to turn." << endl;
}
}