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...