Write a program that reads an MxN matrix A and prints its elements in spiral order.
You should start from the element in the 0th row and 0th column in the matrix and proceed in a spiral order as shown below.

1→ 2→3→ 4

5→ 6→ 7 8
↑ ↓ ↓
9 10←11 12
↑ ↓
13←14←15←16

Output for the above matrix: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

INPUT:
First line contains two integers M and N separated by whitespace. The next M lines contain elements of matrix A, starting with the topmost row. Within each row, the elements are given from left to right.

OUTPUT:
Elements of the matrix printed in a spiral order. All the elements should be separated by whitespace.

CONSTRAINTS:
1 <= M <= 5, 1 <= N <= 5.
Elements in the matrix will be in the range [-100,100]

Show the code you have ... and where you think you are stuck ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.