magic numbers

Hello, I am trying to write a program to compute magic squares. The output is supposed to look like this:

11 18 25 2 9

10 12 19 21 3

4 6 13 20 22

23 5 7 14 16

17 24 1 8 15

however my code produces this output,

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

basically everything is backwards, I have all the right numbers.

Here is the instructions:


Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square, place k+1 into the square right and down, wrapping around the borders. However, if you reach a square that has already been filled, then you must move one square up instead.

Here is my code so far:

//Magic numbers program
//7-24-15

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

int row, col;
int n = 5;
int magic[5][5];



for(int r=0 ; r<n ; r++)

{
for(int c=0 ; c<n ; c++)

{

magic[r][c] = 0;

}

}


row = 1;

col = (n+1)/2;

magic[row - 1][col - 1]=1;



for(int i=2 ; i<=(n*n) ; i++)

{

row-=1;

col-=1;

if(row==0 && col==0)
{

col++; row+=2;

}

else if(row==0) row=n;

else if(col==0) col=n;

else if(magic[row - 1][col - 1]!=0)

{

col++; row+=2;

}


magic[row - 1][col - 1]=i;

}


for(int Row=0; Row<n ; Row++)

{

for(int Col=0; Col<n ; Col++)

{

cout << setw (5) << magic[Row][Col];

}

cout<<endl;

}

return 0;

}






Last edited on
And... what's your question?
Does it work?
Do you get compiler errors?
im not getting the right output.

what I want to get is this:
11 18 25 2 9

10 12 19 21 3

4 6 13 20 22

23 5 7 14 16

17 24 1 8 15


but with the code that I posted I get this output:
15 8 1 24 17
16 14 7 5 23
22 20 13 6 4
3 21 19 12 10
9 2 25 18 11




the code I have works, it compiles but the number order is not what I want. and I cant figure out how to get it right
As far as I can tell it does not matter, right?
I mean, both of them are magic squares...

However, I can only give you 1 solution:
1) flip the matrix upside down and left to right (you'd have to do that yourself)

you on the other hand can do other things:
2) you have to take a closer look at your algorithm.
I don't know this algorithm works so I would have a hard time helping you, I'm sorry.
It's probably something to do with the indices of matrix.
3) you could cheat with the output, just print the values backwards :')
Last edited on
Yes, Gamer2015 is right. Just change the direction of the loops what you use by printing of the values of the matrix.
This works well:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

int row, col;
int n = 5;
int magic[5][5];



for(int r=0 ; r<n ; r++)

{
for(int c=0 ; c<n ; c++)

{

magic[r][c] = 0;

}

}


row = 1;

col = (n+1)/2;

magic[row - 1][col - 1]=1;



for(int i=2 ; i<=(n*n) ; i++)

{

row-=1;

col-=1;

if(row==0 && col==0)
{

col++; row+=2;

}

else if(row==0) row=n;

else if(col==0) col=n;

else if(magic[row - 1][col - 1]!=0)

{

col++; row+=2;

}


magic[row - 1][col - 1]=i;

}


for(int Row=4; Row>-1 ; Row--)

{

for(int Col=4; Col>-1 ; Col--)

{

cout << setw (5) << magic[Row][Col];

}

cout<<endl;

}

return 0;

}


Here you can try it out:
http://ideone.com/KqHxPB

For the next time please use code tags.
Last edited on
Topic archived. No new replies allowed.