Thread: Magic Square Problem

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    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)
    
                    }
    }
    Last edited by GCNDoug; 02-11-2008 at 08:52 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You haven't closed off the for-loop.

    Really: what's the problem?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I know that the for loop has not been closed. The program is not done. I need help completing it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by GCNDoug View Post
    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. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    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"

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    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. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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];
    
    
    }
    Last edited by GCNDoug; 02-12-2008 at 06:05 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.
    Last edited by tabstop; 02-12-2008 at 06:17 PM.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by GCNDoug View Post
    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. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    Last edited by iMalc; 02-13-2008 at 12:44 AM.
    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"

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square Root Problem
    By punter in forum C++ Programming
    Replies: 8
    Last Post: 10-20-2006, 02:46 PM
  2. help on magic square
    By katway in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 06:44 PM
  3. help with magic square
    By dragon_heart in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2004, 05:56 PM
  4. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM
  5. Characters in a Magic Square Program
    By TeamGreen in forum C++ Programming
    Replies: 1
    Last Post: 03-16-2004, 08:08 AM