Can someone explain how this code and algorithm works in depth?
It came from my c++ book that we are using in class,
/*--------------------------------------------------------------------
Program to construct magic squares of odd size.
Input: Size of square (odd #)
Output: A magic square
----------------------------------------------------------------------*/
const int MAX_DIM = 7;
typedef int IntTable[MAX_DIM][MAX_DIM ];
void createMagic(IntTable square, int n);
/*---------------------------------------------------------------------
Construct a magic square of odd size n.
Precondition: size of square is odd positive integer.
Postcondition: square stores an n x n magic square.
----------------------------------------------------------------------*/
void display(IntTable t, int n);
/*---------------------------------------------------------------------
Display an n x n magic square.
Precondition: None.
Postcondition: square is output to cout.
----------------------------------------------------------------------*/
//--- Definition of createMagic()
void createMagic(IntTable square, int n)
{
if (n % 2 == 0)
{
cout << "Size of magic square must be odd.\n";
exit(1);
}
int row,
col;
for (row = 0; row < n; row++)
for (col = 0; col < n; col++)
square[row][col] = 0;
row = 0;
col = n / 2;
for (int k = 1; k <= n * n; k++)
{
square[row][col] = k;