Hi,
I would like to know how to traverse an array spirally?
i.e if I have an array as follows
1 2 3 4
5 6 7 8
9 10 11 12
I want the output to be 1 2 3 4 8 12 11 10 9 5 6 7.
Your question is more appropriate on
In order to solve it, ask yourself how you would solve it with pencil
and paper.
It appears that you would traverse the top and edges and then
increment or decrement the edge as appropriate and then repeat until a
dimension is zero.
Try it symbolically with this matrix that has m rows and n columns:
{col 0} {col 1} {col n-1} {col n}
[A(0,0)] [A(0,1)] ... [A(0,n-1)] [A(0,n)] {row 0}
[A(1,0)] [A(1,1)] ... [A(1,n-1)] [A(1,n)] {row 1}
...
[A(m-1,0)][A(m-1,1)]... [A(m-1,n-1)][A(m-1,n)] {row m-1}
[A(m,0)] [A(m,1)] ... [A(m,n-1)] [A(m,n)] {row m}
Once you figure out how you would do it using two indexes i and j on
paper, do exactly the same thing using C.