Sorry, who wrote "The second half should be similar" ... it is not.
Anyway, here you go:
// traverse array diagonally int c, tmp, x; for (c = N - 1; c > -N; c--) { tmp = N - abs(c) - 1; x = tmp; while (x >= 0) { if (c >= 0) { std::cout << arr[x][tmp - x] << ", "; } else { std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", "; } --x; } std::cout << "\n"; }
Do you need this for a game or something else?
[edit] looking at it again, I think my answer was not very beautifully written. It runs quickly here:
Suppose N is 3.
We need an iteration over coordinate combinations, which looks like this:
(0, 0) (1, 0), (0, 1) (2, 0), (1, 1), (0, 2) (2, 1), (1, 2) (2, 2)
So first, some placeholders:
int c,
Now this outer loop
for (c = N - 1; c > -N; c--) {
does iterating over {2, 1, 0, -1, 2}.
Next step
tmp = N - abs(c) - 1; x = tmp;
turns {2, 1, 0, -1, -2} into {0, 1, 2, 1, 0}, which are the lengths of the required outputs at this stage minus one (therefore, they can be used as indices). We make two copies of this, tmp and x.
Now we are counting from x to 0:
while (x >= 0) { ... --x; }
if we are in the upper left half of arr denoted by c> = 0, the x-indices in arr should start diagonally and go down to zero (from 0 to 0, from 1 to 0 and 2 to 0), while the y-indices should start from zero and go to the diagonal (from 0 to 0, from 0 to 1 and from 0 to 2):
if (c >= 0) { std::cout << arr[x][tmp - x] << ", "; }
when we are in the lower right half, x-indices should start with N and go down to the diagonal (from 2 to 1 and 2 to 2), while y-indices should start from the diagonal and go to N (from 1 to 2 and from 2 to 2):
else { std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", "; }
Finally, we just need a line break at the end of each line:
std::cout << "\n";
Savy ?:-)