Java Magic Square: A n x n matrix that is filled with the numbers 1, 2, 3,.... n
ID: 3741934 • Letter: J
Question
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.
Then test the following features to see if the numbers entered make a magic square:
1. Does each of the numbers 1 - 16 occur in the input? Note: I must perform this check from the ArrayList using the "contains" method.
2. Are the sums of the rows, columns, and diagonals equal to each other? If possible: when you store the number in the ArrayList, store it in a two-dimensional array too.
Here’s a sample run (user input in bold): Enter the numbers 1 - 16 in any order: 12 13 2 7 6 3 16 9 15 10 5 4 1 8 11 14 - You found a magic square! -
Here’s another example: This time it’s not going to work: Enter the numbers 1 - 16 in any order: 2 3 7 10 9 16 4 5 12 8 1 15 14 11 13 6 - The numbers you have entered are not a magic square.
And one more that won’t work: Enter the numbers 1 - 16 in any order: 12 13 2 7 6 3 16 9 16 10 5 4 1 8 11 14 - The numbers you have entered are not a magic
Explanation / Answer
I have made it for N, which can be taken as a input from the user.
If you do not wish it to be dynamic then assign N=4, and remove the code for taking N as input.
following code will check if the values entered are magic or not
-----------------------------------------------------------MagicSquare.java----------------------------------------------------
// JAVA program to check whether a given
// matrix is magic matrix or not
import java.io.*;
import java.util.Scanner;
class MagicSquare {
static int N;
static boolean isMagicSquare(int mat[][])
{
// calculate the sum of
// the prime diagonal
int sum = 0;
for (int i = 0; i < N; i++)
sum = sum + mat[i][i];
// For sums of Rows
for (int i = 0; i < N; i++) {
int rowSum = 0;
for (int j = 0; j < N; j++)
rowSum += mat[i][j];
// check if every row sum is
// equal to prime diagonal sum
if (rowSum != sum)
return false;
}
// For sums of Columns
for (int i = 0; i < N; i++) {
int colSum = 0;
for (int j = 0; j < N; j++)
colSum += mat[j][i];
// check if every column sum is
// equal to prime diagonal sum
if (sum != colSum)
return false;
}
return true;
}
// driver program to
// test above function
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("please enter the size of square");
N = sc.nextInt();
int[][] Matrix = new int[N][N];
System.out.println("Enter the numbers 1 - "+N*N+" in any order");
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
Matrix[i][j] = sc.nextInt();
}
}
if (isMagicSquare(Matrix))
System.out.println("You found a magic square!");
else
System.out.println("The numbers you have entered are not a magic");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.