Q. No. 2. Write a C++ code using nested FOR loops to create an odd number i.e. n x n (e.g. 5x5) matrix implementing the following rules.
1. Place the first number (1) in the middle cell to start.
2. For placing each next number:
(i) Increment the number by 1, increment the column by 1, and decrement the row by 1.
(ii) If you are in the last column, then go to the first column in the same row and decrement the row by 1 and place the number if the cell is empty.
(iii) If you are in the first row, then go to the last row in the same column and increment the column and place the number if the cell is empty.
(iv) If the target cell already has a number, then go back to the immediate previous empty cell in the same order and hierarchy and place the number.
(v) Sample output is as shown in Fig. 4.
0
1
2
3
4
0
9
15
16
22
3
1
14
20
21
2
8
2
19
25
1
7
13
3
24
5
6
12
18
4
4
10
11
17
23
Fig. 4