I have an guaranteed to be a perfect square matrix. I want to start at the center of the matrix in this case it would be matrix[2][2]
, I know how to figure the center (int)(dimensions / 2)
. I need to output the contents of the array in this following outward spiral pattern. Of course the algorithm should work with any perfect square matrix. I wasn't sure if this algorithm already existed and I didn't want to re-invent the wheel.
int dimensions / 2;
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
The output for this example should be
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
See Question&Answers more detail:os