1. Originally Posted by GCNDoug
yeah so If I'm on row 0, and need to go to the other side, I have to ad 2 so that I'm on row 2.
But, row 2 is not necessarily the other side of the matrix. Maybe the other side is row 4, or row 6, or row 8, or row 10, or ....

2. Code:
```#include <iostream>

using namespace std;

typedef	int square[25][25];

square magic = {0};

void gen_Array (square, int);

int main()
{
int size = 0;

cout <<
"Please enter the size of the magic square: " << endl;
cin >> size;

while ((size % 2) == 0)
{
cout <<"Please enter the size of the magic square: " << endl;
cin >> size;
}

gen_Array (magic, size);
}

void gen_Array (square nsquare, int nsize)

{

int nrow = 0;
int ncol = 0;
int row = 0;

int col = (nsize /2);
int total = (nsize * nsize);

nsquare[row][col] = 1;

int i = 0;

for (i = 2; i < total; i++)
{
nrow = row - 1;
ncol = col + 1;
if (nrow == 0 && ncol == (nsize+1))
{
nrow = row + 1;
ncol = nsize;
row = nrow;
col = ncol;
nsquare[row-1][col-1] = i;
}
else
{
if (nrow == 0)
{
nrow = nsize;
}
if(ncol == (nsize+1))
{
ncol = 1;
}
if(nsquare[nrow-1][ncol-1] == 0)
{
row = nrow;
col = ncol;
nsquare[row-1][col-1] = i;
}
else
{
nrow = row + 1;
ncol = col;
if (nrow == (nsize+1))
{
nrow = 1;
}
row = nrow;
col = ncol;
nsquare[row-1][col-1] = i;
}
}
}

int r;
int c;

for (r = 0; r < nsize; r++)
{
cout << endl;
for(c = 0; c < nsize; c++)
cout << nsquare[r][c];
}

}```
Ok I have come up with this now with the help of other websites, however now i am getting a debug error some sort of issue with the array. Anyone see the problem?

3. Code:
```#include <iostream>

using namespace std;

typedef	int square[25][25];

void gen_Array (square, int);

int main()
{
int size = 0;

square magic;

int i ,j;

for (i =0; i<25;i++)

for(j=0;j<25;j++)

magic[i][j] = 0;

cout <<
"Please enter the size of the magic square: " << endl;
cin >> size;

while ((size % 2) == 0)
{
cout <<"Please enter the size of the magic square: " << endl;
cin >> size;
}

gen_Array (magic, size);

return 0;
}

void gen_Array (square nsquare, int nsize)

{

int nrow = 0;
int ncol = 0;
int crow = 0;

int ccol = (nsize /2);
int total = (nsize * nsize);

nsquare[crow][ccol] = 1;

int i = 0;

for (i = 2; i < total; i++)
{

nrow = crow -1;
if (nrow < 0)
nrow = nsize - 1;
ncol = ccol +1;
if (ncol >= nsize)
ncol =0;
if (nsquare[nrow][ncol] != 0)
nsquare[nrow][ncol] = i;
else
{
nrow = crow +1;
ncol = ccol;
nsquare[nrow][ncol] = i;
}
crow = nrow;
ccol = ncol;
}

int r;
int c;

for (r = 0; r < nsize; r++)
{
cout << endl;
for(c = 0; c < nsize; c++)
cout << nsquare[r][c];
}

}```
Well it's been almost a week, I tried another way of doing this and still no luck. Does ANYONE have any ideas. I've tried everything....

4. Originally Posted by GCNDoug
Code:
```		if (nsquare[nrow][ncol] != 0)
nsquare[nrow][ncol] = i;
else
{
nrow = crow +1;
ncol = ccol;
nsquare[nrow][ncol] = i;
}```
Well it's been almost a week, I tried another way of doing this and still no luck. Does ANYONE have any ideas. I've tried everything....
You still have this test backwards -- you should only put a number in a square when you know it's not going to overwrite something, instead of the other way around. Edit to add: and you need to make sure you fill the matrix with zeroes first as well -- that does not happen automatically.

5. I am initializing the matrix to zero, let me try switching the statements under the if with the statements under the else.

6. You don't need to test for zero (or init the array to zero) at all.
You KNOW you will run into a zero every nsize times.
So use (i % nsize == 0) for your test.

7. One more quick question guys, I'm trying to make this loop until the boolean in turned to false so that it will close out of the program

Code:
```while (con = true)
{

cout << "Please enter the size of the magic square: " << endl;
cin >> size;

while ((size % 2) == 0)
{
cout <<"Please enter the size of the magic square: " << endl;
cin >> size;
}

gen_Array (magic, size);

cout << "Would you like to continue? (y or n) " <<endl;
cin >> yon;

if (yon == 'y' || yon == 'Y')
con = true;
if (yon == 'n' || yon == 'N')
con = false;
}```
The yes is working, but the no isn't. Thanks for any help.

8. There is a difference between
Code:
`while (con = true)`
and
Code:
`while (con == true)`
Your compiler would tell you, if you let it. Turn your warnings up.

9. Originally Posted by GCNDoug
Code:
```	for (i = 2; i < total; i++)
{

nrow = crow -1;
if (nrow < 0)
nrow = nsize - 1;
ncol = ccol +1;
if (ncol >= nsize)
ncol =0;
if (nsquare[nrow][ncol] != 0)
nsquare[nrow][ncol] = i;
else
{
nrow = crow +1;
ncol = ccol;
nsquare[nrow][ncol] = i; // Buffer overrun
}
crow = nrow;
ccol = ncol;
}```
Well it's been almost a week, I tried another way of doing this and still no luck. Does ANYONE have any ideas. I've tried everything....
It looks like you're just having trouble following instructions

You don't need two variables to hold each coordinate. If you have nrow and ncol, you don't need crow and ccol. When you move from one cell to anther, there's no need to go back. You don't have to remember where you came from. If you arrive at a cell that is already occupied, then you follow the rest of the rules and keep moving as those rules dictate.
You need to implement the last bullet point in my previous post, which will fix the buffer overrum with nrow that I have commented above.

The line:
Code:
`nsquare[nrow][ncol] = i;`
should only appear once in your code.
This is the kind of simplification process you should use. It does not match your exact example, but it demonstrates an obvious reduction in code duplication. A change like this wont in itself fix your program in any way, but I suggest you study it all the same:
Code:
```if (condition)
{
doA();
doC();
}
else
{
doA();
doB();
doC();
}```
is the same as:
Code:
```doA();
if (!condition)
{
doB();
}
doC();```