#include <stdio.h>
#include <stdlib.h>

void main (int argc, char *argv[])

{
 static int square[15][15];
 int guess, row, column, number;

 guess = atoi (argv[1]);

 if (guess % 2 == 0)
   {
    printf("The number must be odd for the magic square to work. \n");
    exit(0);
   }

 row = 0;
 column = ((guess + 1)/2) - 1;
 square[row][column] = 1;

 for (number = 2; number <= (guess * guess); number++)
   {
    row = row - 1;
    column = column + 1;
    adjust (&row, &column, guess);
    if (square[row][column] != 0)
      {
       row = row + 2;
       column = column - 1;
       adjust(&row, &column, guess);
      }
    square[row][column] = number;
   }

 for (row = 0; row <= (guess - 1); row++)
   {
    for (column = 0; column <= (guess - 1); column++)
      printf(" %3d", square[row][column]);
    printf("\n");
   }
}

adjust (r, c, g)
int *r, *c, g;
{
 if (*r == -1)
   *r = g - 1;
 if (*c == g)
   *c = 0;
 if ((*r == -1) && (*c == g))
   {
    *r = 1;
    *c = g - 1;
   }
}

