# What are these lines of code doing in this program?

• 01-31-2002
Drew
What are these lines of code doing in this program?
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;
}
}
}
• 01-31-2002
Stoned_Coder
This line: int j = (n-1)/2, k, l;

int k,l;
int j= (n-1)/2;

This line: k=(i) ? (i-1) : (n-1);

this could be rewritten as ....

if ( i != 0) k=i-1;
else
k=n-1;

This line: l=(j) ? (j-1) : (n-1);

this similarly could be rewritten as....

if (j != 0) l=j-1;
else
l=n-1;