Can someone explain how this code and algorithm works in depth?

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
----------------------------------------------------------------------*/

#include <iostream> // cin, cout, >>, <<
#include <iomanip> // setw()
#include <cstdlib> // exit()
using namespace std;

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.
----------------------------------------------------------------------*/

int main()
{
IntTable square;
int sizeOfSquare;

cout << "\nEnter size of magic square (odd number): ";
cin >> sizeOfSquare;

createMagic(square, sizeOfSquare);
display (square, sizeOfSquare);
}


//--- 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;

row--;
col++;

if (row < 0 && col >= n)
{
row += 2;
col--;
}

if (row < 0)
row = n - 1;

if (col >= n)
col = 0;

if (square[row][col] != 0)
{
row += 2;
col--;
}
}
}


//--- Definition of display()
void display(IntTable t, int n)
{
const int WIDTH = 4;

cout << "\nMagic square is:\n" << endl;
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
cout << setw(WIDTH) << t[i][j];
cout << endl << endl;
}
}
Topic archived. No new replies allowed.