Magic Square Program (C++)
<p>For those unfamiliar with the classic magic square algorithm: A magic square is a two dimensional array (n x n) which contains a numerical value between the values 1 and n^2 in each location. Each value may appear only once. Furthermore, the sum of each row, column and diagonal must be the same.  The input should be odd as I am writing an odd magic square solution.</p>

<hr>

<p>I have completed the problem but as of now it has an unknown bug (logic? output?) that has been vexing me for the past hour.  The values that are output are very off mark.  Any help would be very much appreciated:</p>

<hr>

<pre><code>#include&lt;iostream&gt;
#include&lt;iomanip&gt;
using namespace std;

int main()
{
  int n;

  cout&lt;&lt; "Please enter an odd integer: ";
  cin&gt;&gt;n;

  int MagicSquare[n][n];


  int newRow,
  newCol;

  // Set the indices for the middle of the bottom i
  int i =0 ;
  int j= n / 2;

  // Fill each element of the array using the magic array
  for ( int value = 1; value &lt;= n*n; value++ )
  {
     MagicSquare[i][j] = value;
     // Find the next cell, wrapping around if necessary.
     newRow = (i + 1) % n;
     newCol = (j + 1) % n;
     // If the cell is empty, remember those indices for the
     // next assignment.
     if ( MagicSquare[newRow][newCol] == 0 )
     {
        i = newRow;
        j = newCol;
     }
     else
     {
        // The cell was full. Use the cell above the previous one.
        i = (i - 1 + n) % n;
     }

  }


  for(int x=0; x&lt;n; x++)
  {
     for(int y=0; y&lt;n; y++)
         cout &lt;&lt; MagicSquare[x][y]&lt;&lt;" ";
     cout &lt;&lt; endl;
  }
}
</code></pre>
