Like Tree1Likes
  • 1 Post By Salem

magic square

This is a discussion on magic square within the C++ Programming forums, part of the General Programming Boards category; Good morning, I'm having trouble trying to debug my magic square program. Here is my code: Code: #include <cstdlib> #include ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    33

    magic square

    Good morning,
    I'm having trouble trying to debug my magic square program. Here is my code:


    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    int *userNum,
         row,
         col,
         number,
         i,
         j,
         k,
         newRow,
         newCol,
         magic[250][250];
    
         userNum = new int;
    
    cout << "Please enter a positive odd integer with a value 3 or more: ";
    cin  >> *userNum;
    
    //********** check to see if my square is an odd integer that's 3 =< x <= 250
    while (1)
    {
    	if (*userNum >= 3 && *userNum <= 250 && *userNum %2 == 1)
    	{
    		break;
    	}
    	else
    	{
    		cout << "Please enter a positive odd integer with a value 3 or more: ";
    		cin >> *userNum;
    	}
    }
    cout << "\n\n";
    
    //******** initializes all matrices to 0
    for (i = 0; i < *userNum; i++)
    {
    	for (j = 0; j < *userNum; j++)
    	{
    		magic[i][j] = 0;
    	}
    }
    
    	row = 0;
    	col = (*userNum / 2);
    	number = 1;
    	magic[row][col] = number;
    
    //******* conditions for set matrices
    for (number = 2; number <= (*userNum) * (*userNum); number++)
    {
    	newRow = row - 1;
    	newCol = col + 1;
    
    	if (newRow < 0)
    	{
    		newRow = *userNum - 1;
    		
    		if (newCol == *userNum)
    		{
    			newCol = 0;
    		}
    	}
    
    	if (magic[newRow][newCol] != 0)
    	{
    		newRow = row + 1;
    	}
    
    	magic[newRow][newCol] = number;
    }
    
    
    //******** prints out results
    k = 0;
    for (i = 0; i < *userNum; i++)
    {
    	for(j = 0; j < *userNum; j++)
    	{
    		if(*userNum <= 250 && *userNum >= 100)
    		{
    			cout << setw(7) << magic[i][j];
    			k++;
    			if(k = *userNum)
    			{
    			cout << endl;
    			}
    		}
    
    		if(*userNum <= 99 && *userNum >= 32)
    		{
    			cout << setw(6) << magic[i][j];
    			k++;
    			if(k = *userNum)
    			{
    			cout << endl;
    			}
    		}
    
    		if(*userNum <= 31 && *userNum >= 10)
    		{
    			cout << setw(5) << magic[i][j];
    			k++;
    			if(k = *userNum)
    			{
    			cout << endl;
    			}
    		}
    
    		else
    		{
    			cout << setw(4) << magic[i][j];
    			k++;
    			if(k = *userNum)
    			{
    			cout << endl;
    			}
    		}
    }
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    }
    When I compile and execute this I get,

    Code:
    Please enter a positive odd integer with a value of 3 or more: 9
    
    0
    0
    0
    0
    1
    0
    0
    0
    0
    any clue as to what is the problem?

    much appreciated.
    Last edited by aromash; 09-24-2011 at 10:01 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    I can see places where you use =, where == would be better.

    Can you explain why userNum is a pointer?
    It seems to be an entirely pointless pointer.

    You might also want to break this into functions, like
    - input
    - solve
    - display

    As opposed to having one long rambling main()
    rogster001 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 4 x 4 Magic Square
    By amorvisa in forum C Programming
    Replies: 2
    Last Post: 10-18-2007, 12:27 AM
  2. Magic Square
    By ilanco in forum C Programming
    Replies: 1
    Last Post: 12-31-2003, 08:07 AM
  3. Magic Square and Tic Tac Toe
    By curlious in forum Game Programming
    Replies: 3
    Last Post: 07-28-2003, 06:50 PM
  4. magic square
    By miki in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2002, 10:26 AM
  5. magic square
    By dizzyhippy in forum C Programming
    Replies: 1
    Last Post: 03-11-2002, 12:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21