This line: int j = (n-1)/2, k, l;
This line: k=(i) ? (i-1) : (n-1);
and
This line: l=(j) ? (j-1) : (n-1);
#include<iostream.h>
#include<iomanip.h>
const int SIZE = 7;
void main(int n)
{
int Magic[SIZE][SIZE];
cin>>n;
if(!(n%2))
{
cout<<"n is even"<<endl;
return;
}
else{
for(int i = 0; i < n; i++)
for(int j=0; j<n; j++) Magic[i][j] = 0;
Magic[0][(n-1)/2]=1;//Middle of first row
//(i,j) is the current position.
int j = (n-1)/2, k, l;
for(int key = 2; key <= n*n; key++){
//Move up and left. Teh next two if statements
//may be replaced by the % operator if -1%n is
//implemented to have value n-1.
k=(i) ? (i-1) : (n-1);
l=(j) ? (j-1) : (n-1);
if (Magic[k][l]) i = (i + 1) %n;
else
{// Magic[k][l] is empty.
i = k;
j = l;
}
Magic[i][j] = key;
}
//Output the magic square
for(i=0; i<n; i++){
for(j=0; j<n; j++)
cout<<setw(5)<<Magic[i][j];
cout<<endl;
}
}
}