-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic Square.cpp
52 lines (50 loc) · 907 Bytes
/
magic Square.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# include <iostream>
# include <ctime>
# include <iomanip>
# include <cstdlib>
using namespace std;
const int rows = 7;
const int cols = 7;
void display(int a[rows][cols])
{
for (int i = 0; i<rows; i++)
{
for (int j = 0; j<cols; j++)
{
cout<<setw(5) << a[i][j] << " ";
}
cout << endl<<endl;
}
}
int main()
{
int magic[rows][cols]={0};
int nRow=0,nCol=0;
int current_row=0;
int current_col=rows/2;
magic[current_row][current_col] = 1;
for(int i=2;i<=rows*rows;i++)
{
nRow=current_row-1;
nCol=current_col+1;
if(current_row-1<0)
{
nRow=rows-1;
}
if(nCol>=cols)
{
nCol=0;
}
if(magic[nRow][nCol]!=0)
{
nRow=current_row+1;
nCol=current_col;
}
magic[nRow][nCol]=i;
current_row=nRow;
current_col=nCol;
}
cout<<" Magic Square of Number "<<rows<<endl<<endl;
cout<<" ----------------------------------------"<<endl;
display(magic);
}