Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A magic square is an arrangement of the numbers from 1 to n 2 in an n x n matrix

ID: 3666586 • Letter: A

Question

A magic square is an arrangement of the numbers from 1 to n2 in an n x n matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same.(NOTE: This sum must be n*(n2+1)/2)

The simplest magic square is the 1x1 magic square whose only entry is the number 1:

1

The next simplest is the 3x3 magic square (all rows, columns, and diagonals add up to 15):

8

1

6

3

5

7

4

9

2

The following is a 5 x 5 magic square (all rows, columns, and diagonals add up to 65):

17

24

1

8

15

23

5

7

14

16

4

6

13

20

22

10

12

19

21

3

11

18

25

2

9

In order to construct an n x n magic square for any odd integer n, you are going to use the following procedure, called the Siamese method:

First, place 1 in the middle of the top row. Then, after integer k has been placed, move up one row and one column to the right to place the next integer k + 1, unless one of the following occurs:

1. If a move takes you above the top row in the jth column, move to the bottom of the jth column and place the integer k + 1 there.
2. If a move takes you outside to the right of the square in the ith row, place k + 1 in the ith row at the left side.
3. If a move takes you to an already filled square or if you move out of the square at the upper right-hand corner, place k + 1 immediately below k.

Click here for a flash animation (will show you how to fill the cells of a 5x5 magic square.)

Your Java program should call a method to generate the magic square and a method to print it. In addition, print the "magic number" that the diagonals, the rows, and the columns add up to. Also, your program should work in a loop, for more than just one magic square. To design the interface, look at the following sample output.

SAMPLE OUTPUT:

Enter the size of magic square (positive & odd): 4
INPUT ERROR!!! Invalid size.
Enter the size of magic square (positive & odd): 3
The magic square with size = 3 is:

    8    1    6
    3    5    7
    4    9    2
The 3x3 magic square adds up to 15

Do you want to continue (Y/N): Y
Enter the size of magic square (positive & odd): 5
The magic square with size = 5 is:

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9
The 5x5 magic square adds up to 65

Do you want to continue (Y/N): y
Enter the size of magic square (positive & odd): 7
The magic square with size = 7 is:

   30   39   48    1   10   19   28
   38   47    7    9   18   27   29
   46    6    8   17   26   35   37
    5   14   16   25   34   36   45
   13   15   24   33   42   44    4
   21   23   32   41   43    3   12
   22   31   40   49    2   11   20
The 7x7 magic square adds up to 175

Do you want to continue (Y/N): n

1

Explanation / Answer

#include "stdafx.h" #include using namespace std; void OddMagicSquare(vector &matrix, int n); void DoublyEvenMagicSquare(vector &matrix, int n); void SinglyEvenMagicSquare(vector &matrix, int n); void MagicSquare(vector &matrix, int n); void PrintMagicSquare(vector &matrix, int n); int main(int argc, char* argv[]) { int n; printf("Enter order of square: "); scanf("%d", &n); vector matrix(n, vector (n, 0)); if (n
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote