Windows crashing Magic Square

This is a discussion on Windows crashing Magic Square within the C++ Programming forums, part of the General Programming Boards category; Okay, heres the deal. I have a magic square program I made for calculating a 3x3 magic square. Code compiles ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    25

    Windows crashing Magic Square

    Okay, heres the deal. I have a magic square program I made for calculating a 3x3 magic square. Code compiles fine, runs almost fine. It starts to get the right values. But then it stops, throws a Windows Error. However, the program finishes , and displays the unfinished magic square. Heres the code. Note: I use MSVC++ for compiler.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int xval, x, size, maxsize;
    	int y, i, j, magic[3][3];
    	int xtemp, ytemp;
    	xval = 1;
    	size = 3;
    	maxsize = size * size;
    	
    
    	for (i = 0; i < size; i++)
    	{
    		for (j = 0; j < size; j++)
    		{
    			magic[i][j] = 0;
    		}	
    	}
    	
    	y = 0;
    	x = size/2;
    	magic[y][x] = xval;
    	xval++;
    	maxsize--;
    	
    	while (maxsize > 0)
    	{
    		xtemp = x;
    		ytemp = y;
    		y--;
    		x++;
    	
    		if (y < 0)
    		{
    			y = size-1;
    		}
    		if (x >= size)
    		{
    			x = 0;
    		}
    		if (magic[y][x] > 0)
    		{
    			ytemp++;
    			magic[ytemp][xtemp] = xval;
    			ytemp = y;
    			xtemp = x;
    		}
    		else if (magic[y][x] == 0)
    		{
    			magic[y][x] = xval;
    		}
    		xval++;
    		maxsize--;
    	}
    	
    	for (i = 0; i < size; i++)
    	{
    		for (j = 0; j < size; j++)
    		{
    			cout <<	"  " << magic [i][j];	
    		}
    	}
    
    	return 0;
    }
    Sorry for the lack of comments, i havent gotten there yet.

    Edit: Feel free to edit the 3 to 5 for a 5x5 magic square. The thing gets even better. I dont understand how its doing what its doing...
    Last edited by KoshiB; 04-19-2006 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,485
    Why don't you debug it and check each of those array references to see if any are out of bounds.
    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.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    x = size/2;
    x is an integer! Why do you do this? (3/2)
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Quote Originally Posted by Ideswa
    Code:
    x = size/2;
    x is an integer! Why do you do this? (3/2)
    MSVC++ automatically round down, and I want the value it gives as the starting position(due to arrays starting at 0).

    The error seems to be some sort of memory error, but i cant figure out where. I tried to debug, but admittedly, i dont know what im doing for debugging.

    EDIT:
    Well, doesn't really matter anymore. This was for a version of the Magic square program I was trying to write for just 3x3 and 5x5 squares (that used no pointers). Ill just stick to the version I wrote that works very well for any sized square that uses pointers.
    Last edited by KoshiB; 04-19-2006 at 12:12 PM.

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    what use do you have for this magic square?
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    OK, so learn to use the debugger. A good start would be to rightclick inside of the function main, choose "insert breakpoint". Then press F5 (run under debugger). It will stop at the line where you have a breakpoint. now use F10 to step through the program line by line. You will see exactly which line blows the program up.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    if (magic[y][x] > 0)
    		{
    			ytemp++;
    			magic[ytemp][xtemp] = xval;
    			ytemp = y;
    			xtemp = x;
    		}
    I suspect that when y == 2 and you assign ytemp == y; (2), then next time through the loop ytemp++ makes it 3 you go out of bounds with magic[ytemp][xtemp]


    x is an integer! Why do you do this? (3/2)

    MSVC++ automatically round down, and I want the value it gives as the starting position(due to arrays starting at 0).
    also I hope you aren't thinking 3/2 = 0 in integer math cuz it doesn't it equals 1 so you are not starting at 0
    Last edited by Darryl; 04-19-2006 at 03:00 PM.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Err, What I want is for the 3/2 to give me one, as one is the middle in a 0,1,2 array. 5/2 gives me 2 which is what I need for a 0,1,2,3,4 array.
    It may be bad practice to do it like that, but it gives me what i need, and easily.

    The Magic square was for a class.

    Thanks for the info on the Debugger. I think itll come in handy.

    Edit: Looking at the program now, i think I may see the problem. Ill have to check it out.
    Last edited by KoshiB; 04-19-2006 at 04:05 PM.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Not familiar with the compiler, but if it is an out of bounds error, just make an apmatrix instead of a normal one. On running it will stop if there is an out-of-bounds error, so you'll know if that's the problem.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Quote Originally Posted by MorphysGhost
    Not familiar with the compiler, but if it is an out of bounds error, just make an apmatrix instead of a normal one. On running it will stop if there is an out-of-bounds error, so you'll know if that's the problem.
    What is an apmatrix?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help an ambitious 16 year old learn C
    By Xlayer in forum C Programming
    Replies: 20
    Last Post: 10-08-2007, 04:15 PM
  2. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  3. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  4. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM

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