Magic Square

I found a code for magic square but it has no output
how do you output this ?

#include<iostream>

using namespace std;

int main()
{
int n;

cout<<"Enter a number: ";
cin>>n;
cout<<"The table is "<<n<<" x "<<n<<endl<<endl;

int x=n/2,y=0,i,j;
int grid[n][n];

for (i=1;i<=n*n;++i)
{
grid[y][x]=i;

x++;
y--;

if (i%n==0)
{
y+=2;
--x;
}
else
{
if (x==n)
{
x==x-n;
}

else if (y<0)
{
y=y+n;
}
}

}


system("pause");
}

first: please use the feature that you can show CODE.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>

using namespace std;

int main()
{
    int n;

    cout<<"Enter a number: ";
    cin>>n;
    cout<<"The table is "<<n<<" x "<<n<<endl<<endl;

    int x=n/2,y=0,i,j;
    int grid[n][n];

    for (i=1;i<=n*n;++i)
    {
        grid[y][x]=i;

        x++;
        y--;

        if (i%n==0)
        {
            y+=2;
            --x;
        }else{
            if (x==n)
            {
                x=0;
            }else if (y<0){
                y+=n;
            }
        }
    }

    system("pause");
}
Last edited on
you output a square with something like this:

1
2
3
4
5
6
for(int i=0;i<n;i++)
{
    for(int t=0;t<n;t++) cout<<grid[i][t]<<" ";

    cout<<endl;
}


if you want to know how exactly this thing works, ask me.
I got it .. :) your output is correct,.. there's something wrong in the code,.. in the else statement,

thank you.
doesn't matter =)
Topic archived. No new replies allowed.