Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Solutions Sem. 03 / 11. Spiral_Numbers_2.cpp
diff options
context:
space:
mode:
1 files changed, 30 insertions, 0 deletions
-rw-r--r--Year 1/Programming introduction/Solutions Sem. 03/11. Spiral_Numbers_2.cpp30
diff --git a/Year 1/Programming introduction/Solutions Sem. 03/11. Spiral_Numbers_2.cpp b/Year 1/Programming introduction/Solutions Sem. 03/11. Spiral_Numbers_2.cpp
new file mode 100644
index 0000000..b7337a4
--- /dev/null
+++ b/Year 1/Programming introduction/Solutions Sem. 03/11. Spiral_Numbers_2.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+
+using namespace std;
+
+int main()
+{
+ int n;
+ cin >> n;
+
+ for (int row = 0, previous = 0; row < n; row++)
+ {
+ int modifier = 1;
+
+ // When going from right to left, make previous be the number at the end of the line + 1, and then decrement
+ if (row % 2 == 1)
+ {
+ previous += n + 1;
+ modifier = -1;
+ }
+
+ for (int col = 0; col < n; col++)
+ cout << (previous += modifier) << " ";
+
+ cout << endl;
+
+ // Account for decrementing when gong from right to left
+ if (row % 2 == 1)
+ previous += n - 1;
+ }
+}