1. ## Magic Square Problem

Hey I'm been working on the standard magic square program. I'm starting to have some trouble and can't get any further then where I left off. I am sooo close, can anyone help me out?

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 &#37; 2) == 0)
{
cout << "Please enter the size of the magic square: " << endl;
cin >> size;
}

gen_Array (magic, size)

}

void gen_Array (int nsquare, int nsize)
{

int nrow = 0;
int ncol = 0;
int crow = 0;
int ccol = (nsize /2);
int total = (nsize * nsize);
square[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 (square[nrow][ncol] != 0)

}
}```

2. You haven't closed off the for-loop.

Really: what's the problem?

3. I know that the for loop has not been closed. The program is not done. I need help completing it.

4. Originally Posted by GCNDoug
I know that the for loop has not been closed. The program is not done. I need help completing it.
Yes, but: what do you need help with? Do you know how to compute the values that go into a magic square? Do you know how to turn this into code? What? Where is the problem?

5. Sorry I guess I figured you guys knew what the magic square was which was a stupid assumption. Essentially all of the rows, columns, and diagonals should equal the same number. I only need it to work with odd dimensions... Apparently it can be done with even cubes but is much harder.

The numbers have to fill a square in a specific pattern.

You start in the center of row one, with the number one. From there you go up one and then to the right before placing the number. You continue doing this until the square is full. If you land on a spot of the array that has already been filled you drop immediately below that number and place the next number in the box below. And then from that position most go up one and then one to the right. I've started to figure out the beginning of the algorithm but now I'm stuck. I was wondering if anyone could help me out.

If the square is 5 x 5 you will fill using the digits one through twenty five.

6. Apart form the missing semicolon, you're going just fine so far.

It doesn't really look to me like you're stuck. You've written code to do exactly what you want and then you suddenly stopped. You know what to do if the spot is non-zero, you drop down one spot, noting that you again may need to wrap around of course. And if it isn't zero, you know that you just have to put the value there and that's all. Given what you've written already I don't see anything that would stop you from writing the rest, since it's no harder.

7. Just remember to remain within the bounds of the square. If the computed value of the row or column is outside the bounds (including -1), then you have to check for that before actually checking to see if there is a value there, or placing a value there. Otherwise you may crash the program or get invalid results.

8. hmmmm I am still having trouble this is what I've come up with for the final code at the moment. Any ideas?

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 &#37; 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 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
nsquare[nrow-1][ncol] = i;
}

int r = 0;
int c = 0;

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

}```

9. You've got your if-test backwards, it looks like. And I thought if the square was taken, you had to go down, not up. You should also test that going down doesn't need to wrap around.

You don't need to set r=0 and c=0 twice after filling in the grid; once is enough. You may want to print some spaces, too.

Edit: Oh, and you're going to print one extra row and one extra column with your for-loops.

10. hmmm I switched the if statements however I am still only printing one row of five zero's. Sorry I'm not that good at programming; I am extremely confused now... lol

11. Originally Posted by GCNDoug
hmmm I switched the if statements however I am still only printing one row of five zero's. Sorry I'm not that good at programming; I am extremely confused now... lol
Now that I notice, you have the indentation right but not the braces; everything you want to be included in the for-loop on r needs to be enclosed in
Code:
`{curly braces}`
You still need to also fix all the other errors if you want actual right answers.

12. You're not following through from what you've got in pseudocode to the source code.
You can't just write to nrow-1. There are more than two times where you have to shift your focus to another cell.
Think about the case where you have just filled in the top-right corner cell. You need to move diagonally up to the right, right?
• First you move up, and you notice that you've gone off the top edge, so you wrap around to the bottom.
• Next you move right, and you notice that you've gone off the grid to the right this time, so you wrap around back to the left hand edge.
• Now you notice that the cell you're looking at already has a number in it. (Trust me there always is in this particular case) So, the rules say to move down one row.
• Now you again notice that you've gone off the bottom of the grid, so you have to wrap back to the top.

And finally you're on a spot which has not got a number in it yet, and is on the grid (the top-left position in this case). Now we write the number, and loop round to start following the same procedure all over again for the next number. You'll notice that every one of the above four bullet points requires possibly shifting your focus to another cell. You've so far only done the first two. Now you should be able to finish this.

You really should have followed this through on paper first, as you would probably have worked out all the above by actually doing it on paper.

13. I tired rewriting the algorithm after working though it on paper and still no luck. I really don't understand what's going on. I feel like I've translated the steps right I must have just applied them wrong... Any ideas?

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++)
{

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

nsquare[nrow][ncol] = i;

col = ncol;
row = nrow;

}

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

}```

14. 1. Why are you stepping by two all of a sudden? When you walk off the edge, you want to go to the other side.
2. When you chain things together with if-else if-else if-else if, then only one of those steps will happen, even though you want all of them to happen.

15. 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.