C++ Modify the code to print 2D matrix in spiral order starting from center

441 views Asked by At

I have an assignment which is: Print 2D matrix in spiral order starting from top left corner and then starting from center (end point from first assignment will be the starting point).

I have made the traversal from top left corner, no problem, but I can`t modify the code to do the opposite. Any thoughts?

for (int n = 0, i = 0, j = 0, rightWall = size - 1, leftWall = 0; n < size * 
size; n++) {
       if ((i == (leftWall + 1)) && (j == leftWall)) {rightWall--; 
leftWall++;} //SHRINK SPIRAL
       if ((j == rightWall) && (i < rightWall)) {i++; continue;} //GO DOWN
       if ((j < rightWall) && (i == leftWall)) {j++; continue;} //GO RIGHT
       if ((i == rightWall) && (j > leftWall)) {j--; continue;} //GO LEFT
       if ((j == leftWall) && (i > leftWall)) {i--; continue;} //GO UP
}
0

There are 0 answers