Thread: Windows crashing Magic Square

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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