This method is useful for calculating magic squares with odd order. It begins by placing a 1 in the center of the top row), then incrementally placing subsequent numbers in the square one unit diagonally up and to the right. The counting is wrapped around if the number exits the boundaries of the array). When a square is encountered which is already filled, the next number is instead placed below the previous one and the method continues.

I've researched how to do it, but I can't get it to work. I get the following errors upon compiling in Unix using g++:

MagicSquare.cpp: In function `int main()':

MagicSquare.cpp:18: error: `n' cannot appear in a constant-expression

I used what I could learn from http://www.geocities...eload_coolmenus

My code is as follows:

#include <iostream> using namespace std; int main(void) { int n; cout<<"Enter Magic Square Order: "; cin>>n; cout<<endl; int squareSize=n*n; int r=0, c=n/2; int **matrix; matrix = new int*[n]; for (int i=1;i<=3;i++) { matrix = new int[n][n]; } for (int k=1; k<=squareSize; ++k) { matrix[r][c] = k; r--; c++; if (k%n == 0) { r += 2; --c; } else { if (c==n) c -= n; else if (r<0) r += n; } } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cout<< matrix[i][j] << " "; } cout<< endl; } delete [] matrix; }

Thank you.