This is a discussion on Magic Square Problem within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by GCNDoug yeah so If I'm on row 0, and need to go to the other side, I ...
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?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]; } }
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....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]; } }
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.
I am initializing the matrix to zero, let me try switching the statements under the if with the statements under the else.
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.
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
The yes is working, but the no isn't. Thanks for any help.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; }
There is a difference between
andCode:while (con = true)
Your compiler would tell you, if you let it. Turn your warnings up.Code:while (con == true)
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:should only appear once in your code.Code:nsquare[nrow][ncol] = i;
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:
is the same as:Code:if (condition) { doA(); doC(); } else { doA(); doB(); doC(); }Code:doA(); if (!condition) { doB(); } doC();
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"