The Spiral Matrix problem takes a 2-Dimensional array of order M x N as an input, and prints the elements of this matrix in spiral order. This code is written in C++.
. Traverse the first row and print the elements of the first row from left to right,
then increment in the first row.
. Now, traverse the last column and print the elements from top to bottom,
and then decrement in the last column.
. traverse the bottom row and print the elements from the right to left,
after that decrement in the last row.
. traverse the first column and print the elements from bottom to top,
then increment in first column and so on.
Input format: 4 4
Output format: 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Spiral Print of 2-D array: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Submitted by Ravi Kumar Mehta (ravi)
Download packets of source code on Coders Packet
Comments