Thread: Magic Square Problem

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by GCNDoug View Post
    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. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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. #18
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by GCNDoug View Post
    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. #20
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I am initializing the matrix to zero, let me try switching the statements under the if with the statements under the else.

  6. #21
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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. #22
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #24
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by GCNDoug View Post
    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();
    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"

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