A magic square is an arrangement of the numbers from 1 to n2 in an nxn matrix, w
ID: 3568519 • Letter: A
Question
A magic square is an arrangement of the numbers from 1 to n2 in an nxn 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. It is not hard to show that this sum must be n(n2+1)/2. Here is an example 4x4 magic square. Notice that all of the rows add up to 34 [which is (4*(42+1))/2] , all of the columns add up to 34, and both of the main diagonals add up to 34. Also notice that each of the numbers from 1 through 16 is used only once each in the square and that only the numbers 1 through 16 are used.
1 2 15 16
13 14 3 4
12 7 10 5
8 11 6 9
For this program, you are going to allow the user to enter the values for a 4x4 magic square and then validate the rows, columns and diagonals of the matrix to see if it is a magic square or not.
Your program will process 4x4 squares, though it should be easy to make your program handle any nxn array. Your program will prompt for the user to enter the four values for each row, row-by-row. Your program should output complete, detailed reasons as to why the square isn
For this program, you are going to allow the user to enter the values for a 4x4 magic square and then validate the rows, columns and diagonals of the matrix to see if it is a magic square or not.
Your program will process 4x4 squares, though it should be easy to make your program handle any nxn array. Your program will prompt for the user to enter the four values for each row, row-by-row. Your program should output complete, detailed reasons as to why the square isn
Explanation / Answer
import java.util.Scanner;
public class MagicSquare {
public static void main(String... args){
int[][] arr = new int[4][4];
int size = arr.length,i=0,j;
String[] str = new String[4];
Scanner scr = new Scanner(System.in);
System.out.println("The magic value for your square is 34, which means that every row, " +
"column and diagonal of your square must add up to that number.");
for(;i<size;i++){
System.out.print("Please enter the 4 values for row " + i + ",separated by spaces: ");
str = scr.nextLine().split(" ");
if (str.length>4){
System.out.println("Only four values please");
System.exit(0);
}
j=0;
for(String temp : str){
arr[i][j] = Integer.parseInt(temp);
j++;
}
}
boolean diag,row,col,range;
System.out.print("Checking square for problems:");
diag = checkDiagonals(arr);
row = checkRows(arr);
col = checkColumns(arr);
range = checkRange(arr);
if(diag && row && col && range){
System.out.println(" MAGIC: YES");
}
else{
System.out.println(" MAGIC: NO");
}
}
public static boolean checkRows ( int theSquare[][] ){
int size = theSquare.length , magicNumber = size * (((size * size) + 1))/2, sum, flag = 1;
System.out.print(" ROWS: ");
for(int i = 0; i<size;i++){
sum = 0;
for(int j = 0;j<size;j++){
sum+=theSquare[i][j];
}
if(sum != magicNumber){
flag = -1;
System.out.print(i + " ");
}
}
if(flag == 1){
System.out.print("VALID");
return true;
}
else{
return false;
}
}
public static boolean checkColumns( int theSquare[][] ){
int size = theSquare.length , magicNumber = size * (((size * size) + 1))/2, sum, flag = 1;
System.out.print(" COLS: ");
for(int i = 0; i<size;i++){
sum = 0;
for(int j = 0;j<size;j++){
sum+=theSquare[j][i];
}
if(sum != magicNumber){
flag = -1;
System.out.print(i + " ");
}
}
if(flag == 1){
System.out.print("VALID");
return true;
}
else{
return false;
}
}
public static boolean checkDiagonals ( int theSquare [][] ){
int size = theSquare.length , magicNumber = size * (((size * size) + 1))/2, sum = 0, flag = 1;
System.out.print(" DIAG: ");
for(int i = 0;i<size;i++){
sum+=theSquare[i][i];
}
if(sum != magicNumber){
flag = -1;
System.out.print(0 + " ");
}
sum=0;
for(int j=0,i=3;j<size;j++,i--){
sum+=theSquare[i][j];
}
if(sum != magicNumber){
flag = -1;
System.out.print(1 + " ");
}
if(flag == 1){
System.out.print("VALID");
return true;
}
else
return false;
}
public static boolean checkRange ( int theSquare [][] ){
int size = theSquare.length, dim = size * size,flag=1;
int[] checker = new int[dim];
System.out.print(" RANGE: ");
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
if( theSquare[i][j] >=1 && theSquare[i][j] <= dim) {
if (checker[theSquare[i][j] - 1] == 0) {
checker[theSquare[i][j] - 1] = -1;
}
else{
flag = -1;
System.out.print(theSquare[i][j] + " ");
}
}
else{
flag = -1;
System.out.print(theSquare[i][j] + " ");
}
}
}
if(flag == 1){
System.out.print("VALID");
return true;
}
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.