Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Solutions Sem. 03 / 11. Spiral_Numbers_2.cpp
blob: 5dc5d5b6ade23543dae4ce8f2ec3590b8aeaa509 (plain) (blame)
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
#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, previous = number at the end of the line + 1, and make further number operations 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;
	}
}