Question

Magic Squares; Java code

Magic squares. An n × 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. For example,
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Write a program that reads in n2 values from the keyboard and tests whether they form a magic square
when arranged as a square matrix. You need to test three features:
• Did the user enter n2 numbers for some n?
• Do each of the numbers 1, 2, ..., n2 occur exactly once in the user input?
• When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to
each other?
If the size of the input is a square, test whether all numbers between 1 and n2 are present. Then compute
the row, columns, and diagonal sums. Implement a class Square with methods
public void add(int i)
public boolean isMagic()
0 0
Add a comment Improve this question Transcribed image text
Answer #1
/************************************************************************* * Compilation: javac MagicSquare.java * Execution: java MagicSquare N * *Generates a magic square of order N. A magic squares is an N-by-N * matrix of the integers 1 to N^2, such that all row, column, and * diagonal sums areequal. * * One way to generate a magic square when N is odd is to assign * the integers 1 to N^2 in ascending order, starting at the * bottom, middle cell.Repeatedly assign the next integer to the * cell adjacent diagonally to the right and down. If this cell * has already been assigned another integer,instead use the * cell adjacently above. Use wrap-around to handle border cases. * * * % java MagicSquare 3 * 4 9 2 * 3 5 7 * 8 1 6 * * % java MagicSquare5 * 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 * * Limitations * ----------- * - N must be odd **************************************************************************/ public class MagicSquare { public static void main(String[] args) { int N =Integer.parseInt(args[0]); if (N % 2 == 0) throw new RuntimeException("N must be odd"); int[][] magic = new int[N][N]; int row = N-1; int col = N/2;magic[row][col] = 1; for (int i = 2; i <= N*N; i++) { if (magic[(row + 1) % N][(col + 1) % N] == 0) { row = (row + 1) % N; col = (col + 1) % N; } else {row = (row - 1 + N) % N; // don't change col } magic[row][col] = i; } // print results for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if(magic[i][j] < 10) System.out.print(" "); // for alignment if (magic[i][j] < 100) System.out.print(" "); // for alignmentSystem.out.print(magic[i][j] + " "); } System.out.println(); } }}
answered by: Bruna
Add a comment
Know the answer?
Add Answer to:
Magic Squares; Java code
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement...

    Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement of the numbers 1, 2, ., N2, in which the sum of rows, columns, and diagonals are the same. The users (i.e., TAs) will specify the size of the square matrix: N. The value N must be an odd number between 3 and 15. Example Run For example, you program is expected to run as the following way. NOTE: We only list 5 magic...

  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • Java Magic Square: A n x n matrix that is filled with the numbers 1, 2,...

    Java Magic Square: A n x n matrix that is filled with the numbers 1, 2, 3,.... n^2 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. I need to write an application that gets 16 values as user input, in any order. Store these values in an ArrayList. When the numbers are put into a square, this would be a 4x4 two-dimensional array....

  • C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is...

    C++ 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...

  • I need this written in C++. Magic Squares. An n x n matrix that is filled...

    I need this written in C++. 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,...

  • 8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic...

    8. Ann 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 main diagonals is the same value. For example, 16 3 213 5 10 11 8 9 6 7 12 4 15 14 1 Write the program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4...

  • Help pls! and kindly explain how you do this as well. Thaank you Write a program...

    Help pls! and kindly explain how you do this as well. Thaank you Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties: the grid contains only the numbers 1 through 9 the sum of each row, each column, and each diagonal all add up to the same number Notes: I have provided the...

  • 9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...

    9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown in the following figure · 15 4 9 2-15 3 5 7-15 81 615 15 15 15 15 Write a program that simulates a magic square using...

  • please use java language please used ArrayList The Lo Shu Magic Square is a grid with...

    please use java language please used ArrayList The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The • The sum of each row, each column, and each diagonal all add up to the same number 20. Lo Shu Magic Square Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. This is shown in Figure 7-32. In a program you can simulate a...

  • how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagona...

    how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagonals have the same sum. For example, A- 3 5 7 4 9 2 Complete the following magic matrices 17? ?? 3 ? 2 ? 2? ? Do the following steps in each case: 1. Write the system of equations and put it under the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT