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

Program Specification An n x n matrix that is filled with the whole numbers 1, 2

ID: 3591601 • Letter: P

Question

Program Specification An n x n matrix that is filled with the whole 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 Here is a magic square where n-3 4 Write a program that reads n numbers from standard input and tests whether they form a magic square when put into matrix form. The value of n is NOT an input to the program; n must be determined from the number of inputs. For example, the input that creates the example matrix above is: 816 357492 The output is a single word, "true" if the input produces a magic square, "false" otherwise. Your program may assume that each input token is a whole number The program must verify: The proper number of input values was provided. Each of the numbers between 1 and n' occurs exactly once in the input. When the numbers are arranged in a matrix, the sum of the 2. e rows, ecolumns and diagonals must be the same value

Explanation / Answer

CheckMagicSquare.java

import java.util.Arrays;
import java.util.Scanner;

public class CheckMagicSquare {

public static void main(String[] args) {

// Declaring variables
int n;
String str;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter elements :");
str = sc.nextLine();

String arr[] = str.split(" ");
n = (int)(Math.sqrt(arr.length));

int i = 0;
int[][] square = new int[n][n];
// here This Nested loop will take the user given values and fill the
// two-dimensional array
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
square[row][col] = Integer.parseInt(arr[i]);
i++;
}
}
// Calling the method to check the matrix is a magic square or not.
boolean bool = isMagic(square);

//Displaying the output
System.out.println(bool);

}

// Method code which contains logic which checks whether the matrix is a
// magic square or not.
private static boolean isMagic(int[][] square) {
int order = square.length;
int[] sumRow = new int[order];
int[] sumCol = new int[order];
int[] sumDiag = new int[2];
Arrays.fill(sumRow, 0);
Arrays.fill(sumCol, 0);
Arrays.fill(sumDiag, 0);
// calculating the Sum of Each row
for (int row = 0; row < order; row++) {
for (int col = 0; col < order; col++) {
sumRow[row] += square[row][col];
}

}
// calculating the Sum of Each column
for (int col = 0; col < order; col++) {
for (int row = 0; row < order; row++) {
sumCol[col] += square[row][col];
}

}
// calculating the Sum of Diagonal 1
for (int row = 0; row < order; row++) {
sumDiag[0] += square[row][row];
}

// calculating the Sum of Diagonal 2
for (int row = 0; row < order; row++) {
sumDiag[1] += square[row][order - 1 - row];
}

boolean bool = true;
int sum = sumRow[0];
for (int i = 1; i < order; i++) {
bool = bool && (sum == sumRow[i]);
}
for (int i = 0; i < order; i++) {
bool = bool && (sum == sumCol[i]);
}
for (int i = 0; i < 2; i++) {
bool = bool && (sum == sumDiag[i]);
}

return bool;

}

}

_________________

Output:

Enter elements :8 1 6 3 5 7 4 9 2
true

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote