I was given this assignment to do, but the pseudo-code doesn't make much sense to me, can anyone interpret this for me. (i'll explain why i'm confused down below)
Implement the following algorithm to construct magic n × n squares; it works only if n is odd.
Set row = n - 1, column = n / 2.
For k = 1 . . . n * n
Place k at [row][column].
Increment row and column.
If the row or column is n, replace it with 0.
If the element at [row][column] has already been filled
Set row and column to their previous value.
Decrement row.
Here is the 5 × 5 square that you get if you follow this method:
11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15
Write a program whose input is the number n and whose output is the magic square of order n if n is odd. (Horstmann, 2017-08-12, p. EX6-3)
So i find this pseudo-code confusing because:
1. its starts out with saying "set row = n - 1, column = n / 2. but with an array aren't row and column values supposed to be constant?
2. "For k = 1...n * n" is very confusing, it seems like the start of a for loop, but what follows after it isn't clear at all (at least to me)
3. Now it seems to be asking me to place "k" in the matrix, but i have no idea what k should be.
1. It's not the dimension of the array but start indexes. The dimension (as a magic sqare) is n x n.
2. I would guess they mean that k is the index (incremented by 1) that goes from 1 to n*n.
3. See 2,