Starting from:

$30.99

CS217- Magic Square Solved

Question: To write a program for generating magic square of odd size using pointer arithmetic.

An 3x3 normal magic square is an arrangement of the numbers 1, 2, 3, ... n2 in a square array, with the property that the sum of every row and column, as well as both diagonals, is the same number.  An example of a 3x3 normal magic square is

6
1
8
7
5
3
2
9
4
You can verify that each of the three rows, the three columns, and the two diagonals add to 15.

Algorithm: To build a magic square we will be using  Loubere's algorithm which is as follows:

Step 1: Begin by placing a 1 in the middle location of the top row:

 

 
1
 
 
 
 
 
 
 
Step 2: We then write successive integers in an upward-right diagonal path (i.e. in the right column of row above current inserted element position), with the following special cases:

When this upward-right movement would result in a location outside the boundaries of the square, we place the new number at the opposite end of the row or column that would contain the new number, if the rows and columns were not bounded.
If the upward-right square is already occupied, place the new number directly below the current one.
In the 3x3 case, this gives us the following sequence of placements:

1.Place 1 in the middle location of the top row.

 
1
 
 
 
 
 
 
 
Next place 2 on the above row and right column (Step 2 a). Since there is no row above first row so we place the 2 in the last row and next column.
 
1
 
 
 
 
 
 
2
Next place 3 on the above row and right column (Step 2 a). Since there is no right column row so we place the 3 in the previous row and first column.
 
1
 
3
 
 
 
 
2
Next place 4 on the above row and right column (Step 2 a). Since this place is already occupied place 4 below 3 (Step 2 b).
 
1
 
3
 
 
4
 
2
Next place 5 on the above row and right column of number 4 (Step 2 a).
 
1
 
3
5
 
4
 
2
Next place 6 on the above row and right column of number 5 (Step 2 a).
 

 
1
6
3
5
7
4
 
2
Next place 7 on the above row and right column of number 6 (Step 2 a). Since this place is already occupied by 4 place 7 below 6.
 
1
6
3
5
7
4
 
2
Next place 8 on the above row and right column of number 7 (Step 2 a). Since this place is already outside the boundaries, place 8 in the first row and first column.
8
1
6
3
5
7
4
 
2
Finally, place 9 on the above row and right column of number 8 (Step 2 a). Since this place is already outside the boundaries as there is no row above first row so we place the 9 in the last row and next column to complete our magic square.
8
1
6
3
5
7
4
9
2
 

Now see if you can follow this algorithm to build a 5x5 magic square (Follow step by step to build the magic ).

17 24 1      8    15

23 5     7    14 16

4     6     13 20 22

12 19 21 3
18 25 2 9
Once done your goal is to write the code for generating a magic square of odd size using the above algorithm. Make sure that the sum of all of the rows of generated magic square and sum of all of its columns and diagonals must be same. ()

More products