The instructions specify \"The value of n is NOT an input to the program; n must
ID: 3863549 • Letter: T
Question
The instructions specify "The value of n is NOT an input to the program; n must be determined from the number of inputs. "
Coding Language: JAVA
Program Specification: An n x n matrix that is filled with the whole numbers 1, 2, 3, n 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 33: 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 8 1 6 3 57 49 2 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. 2. 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 rows, columns, and diagonals must be the same value.Explanation / Answer
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws FileNotFoundException, IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Reading the string and converting into array of integers
String input[] = br.readLine().trim().split(" ");
int a[] = new int[input.length];
for(int i=0;i<input.length;i++){
a[i] = Integer.parseInt(input[i]);
}
//Calculate n
int n = (int)Math.sqrt(a.length);
//Checking whether n is valid
if(n*n!=input.length){
System.out.println("false");
return;
}
int ind = 0;
int magic[][] = new int[n][n];
HashSet<Integer> hs = new HashSet();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
magic[i][j] = a[ind++];
//Number out of range
if(magic[i][j]>n*n){
System.out.println("false");
return;
}
//Duplicate numbers are entered
if(hs.contains(magic[i][j])){
System.out.println("false");
return;
}
hs.add(magic[i][j]);
}
}
//Calculating the sum of a single row
int s = 0,t;
for(int i=0;i<n;i++){
s += magic[0][i];
}
//Checking for sum of rows
for(int i=0;i<n;i++){
t = 0;
for(int j=0;j<n;j++){
t += magic[i][j];
}
if(t!=s){
System.out.println("false");
return;
}
}
//Checking for sum of columns
for(int i=0;i<n;i++){
t = 0;
for(int j=0;j<n;j++){
t += magic[j][i];
}
if(t!=s){
System.out.println("false");
return;
}
}
//Checking for diagonal sums
t = 0;
for(int i=0;i<n;i++){
t = t+magic[i][i];
}
if(t!=s){
System.out.println("false");
return;
}
t = 0;
for(int i=0;i<n;i++){
t = t+magic[i][n-i-1];
}
if(t!=s){
System.out.println("false");
return;
}
System.out.println("true");
}
}
OUTPUT :
8 1 6 3 5 7 4 9 2
true
1 2 2 3
false
1
true
1 2
false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.