The way I would do it is to use a single loop: 1 to N (where N is the number of elements).
I'd also add a
x
and
y
initially set to zero, and a
deltaX
and
deltaY
set to 1 and 0 respectively.
Each time I went round the loop, I'd put the loop counter value into the array at
(x, y)
and change x and y by their respective delta values.
Then check: is
x
right of the array? If it is, then
x
moves back one value, and deltaX becomes 0.
y
is incremented by one, and deltaY is set to one.
Then check
y
for it's limit and start it moving to the left where necessary.
Then check x for the left hand limit, and so forth.
If you think about it, that's pretty much the way you do it yourself!
Give it a try on paper and you'll see what I mean.
If you are having problems getting started at all, then this may help:
How to Write Code to Solve a Problem, A Beginner's Guide[
^]