Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n
ID: 3662331 • Letter: M
Question
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.
Write a program that reads in 16 values from the keyboard and tests
whether they form a magic square when put into a 4 × 4 array.
4 15 14 1
9 6 7 12
5 10 11 8
16 3 2 13
You need to test two features:
1. Does each of the numbers 1, 2, ..., 16 occur in the user input?
2. When the numbers are put into a square, are the sums of the rows, columns,
and diagonals equal to each other?
Explanation / Answer
import java.util.Scanner;
public class MagicSqure {
public static void main(String[] args) {
int[][] a = new int[4][4];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out
.print("Enter the value for a[" + i + "][" + j + "]:");
a[i][j] = scanner.nextInt();
}
}
System.out.println("User Matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
if (matchNumbers(a)) {
if (isMagicSquare(a)) {
System.out.println("Magic Square");
} else {
System.out.println("Not Magic Square");
}
} else {
System.out.println("Invalid Square");
}
// System.out.println(isMagicSquare(a));
}
/**
* method to check each of the numbers 1, 2, ..., 16 occur in the user input
*
* @param a
* @return
*/
public static boolean matchNumbers(int a[][]) {
int matches = 0;
int[][] square = new int[4][4];
int count = 1;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
square[i][j] = count++;
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 4; n++) {
int value = a[m][n];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (square[i][j] == value) {
matches++;
square[i][j] = 0;
}
}
}
}
}
if (matches == 16) {
return true;
} else {
return false;
}
}
/**
* method to check are the sums of the rows, columns,and diagonals equal to
* each other(magic square or not
*
* @param arr
* @return
*/
public static boolean isMagicSquare(int[][] arr) {
final int n = arr.length;
final int nSquare = n * n;
final int M = (n * n * (n * n + 1) / 2) / n;
int sumOfRow = 0, sumOfColoumns = 0, sumOfPrimaryDiagonal = 0, sumOfSecondaryDiagonal = 0;
boolean[] flag = new boolean[n * n];
for (int row = 0; row < n; row++) {
sumOfRow = 0;
sumOfColoumns = 0;
for (int col = 0; col < n; col++) {
if (arr[row][col] < 1 || arr[row][col] > nSquare)
return false;
if (flag[arr[row][col] - 1] == true)
return false;
flag[arr[row][col] - 1] = true;
sumOfRow += arr[row][col];
sumOfColoumns += arr[col][row];
}
sumOfPrimaryDiagonal += arr[row][row];
sumOfSecondaryDiagonal += arr[row][n - row - 1];
if (sumOfRow != M || sumOfColoumns != M)
return false;
// System.out.println(M);
}
if (sumOfPrimaryDiagonal != M || sumOfSecondaryDiagonal != M)
return false;
return true;
}
}
OUTPUT1:
Enter the value for a[0][0]:1
Enter the value for a[0][1]:2
Enter the value for a[0][2]:3
Enter the value for a[0][3]:4
Enter the value for a[1][0]:5
Enter the value for a[1][1]:6
Enter the value for a[1][2]:7
Enter the value for a[1][3]:8
Enter the value for a[2][0]:9
Enter the value for a[2][1]:10
Enter the value for a[2][2]:11
Enter the value for a[2][3]:12
Enter the value for a[3][0]:13
Enter the value for a[3][1]:14
Enter the value for a[3][2]:15
Enter the value for a[3][3]:16
User Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Not Magic Square
OUTPUT2:
Enter the value for a[0][0]:4
Enter the value for a[0][1]:15
Enter the value for a[0][2]:14
Enter the value for a[0][3]:1
Enter the value for a[1][0]:9
Enter the value for a[1][1]:6
Enter the value for a[1][2]:7
Enter the value for a[1][3]:12
Enter the value for a[2][0]:5
Enter the value for a[2][1]:10
Enter the value for a[2][2]:11
Enter the value for a[2][3]:8
Enter the value for a[3][0]:16
Enter the value for a[3][1]:3
Enter the value for a[3][2]:2
Enter the value for a[3][3]:13
User Matrix:
4 15 14 1
9 6 7 12
5 10 11 8
16 3 2 13
Magic Square
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.