Good morning,
I'm having trouble trying to debug my magic square program. Here is my code:
Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int *userNum,
row,
col,
number,
i,
j,
k,
newRow,
newCol,
magic[250][250];
userNum = new int;
cout << "Please enter a positive odd integer with a value 3 or more: ";
cin >> *userNum;
//********** check to see if my square is an odd integer that's 3 =< x <= 250
while (1)
{
if (*userNum >= 3 && *userNum <= 250 && *userNum %2 == 1)
{
break;
}
else
{
cout << "Please enter a positive odd integer with a value 3 or more: ";
cin >> *userNum;
}
}
cout << "\n\n";
//******** initializes all matrices to 0
for (i = 0; i < *userNum; i++)
{
for (j = 0; j < *userNum; j++)
{
magic[i][j] = 0;
}
}
row = 0;
col = (*userNum / 2);
number = 1;
magic[row][col] = number;
//******* conditions for set matrices
for (number = 2; number <= (*userNum) * (*userNum); number++)
{
newRow = row - 1;
newCol = col + 1;
if (newRow < 0)
{
newRow = *userNum - 1;
if (newCol == *userNum)
{
newCol = 0;
}
}
if (magic[newRow][newCol] != 0)
{
newRow = row + 1;
}
magic[newRow][newCol] = number;
}
//******** prints out results
k = 0;
for (i = 0; i < *userNum; i++)
{
for(j = 0; j < *userNum; j++)
{
if(*userNum <= 250 && *userNum >= 100)
{
cout << setw(7) << magic[i][j];
k++;
if(k = *userNum)
{
cout << endl;
}
}
if(*userNum <= 99 && *userNum >= 32)
{
cout << setw(6) << magic[i][j];
k++;
if(k = *userNum)
{
cout << endl;
}
}
if(*userNum <= 31 && *userNum >= 10)
{
cout << setw(5) << magic[i][j];
k++;
if(k = *userNum)
{
cout << endl;
}
}
else
{
cout << setw(4) << magic[i][j];
k++;
if(k = *userNum)
{
cout << endl;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
}
When I compile and execute this I get,
Code:
Please enter a positive odd integer with a value of 3 or more: 9
0
0
0
0
1
0
0
0
0
any clue as to what is the problem?
much appreciated.