Output:
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12
The idea is to use loops similar to the program for printing a matrix in spiral form. One by one rotate all rings of elements, starting from the outermost. To rotate a ring, we need to do following.
1) Move elements of top row.
2) Move elements of last column.
3) Move elements of bottom row.
4) Move elements of first column.
Input 1 2 3 4 5 6 7 8 9
Output: 4 1 2 7 5 3 8 9 6
For 4*4 matrix Input: 1 2 3 4
5 6 7 8 9 10 11 12 13 14 15 16
Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12 The idea is to use loops similar to the program for printing a matrix in spiral form. One by one rotate all rings of elements, starting from the outermost. To rotate a ring, we need to do following.
1) Move elements of top row. 2) Move elements of last column. 3) Move elements of bottom row. 4) Move elements of first column.