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

Need help make a flow chart for Magic square in Java.(if not on computer than an

ID: 3782083 • Letter: N

Question

Need help make a flow chart for Magic square in Java.(if not on computer than anything hand drawn which kind of gives an idea pls)

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:

8 1 6

3 5 7

4 9 2

Need a flow chart for program that reads n2 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 5 7 4 9 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:

1. The proper number of input values was provided.

2. Each of the numbers between 1 and n2 occurs exactly once in the input.

3. When the numbers are arranged in a matrix, • the sum of the rows, • columns, • and diagonals must be the same value.

Create a flow chart and identify a list of modules/functions/methods and data structures required for implementation in Java.

Explanation / Answer

========================================================

import java.util.*;
class Magic{
public static boolean isMagicSquare(int[][] arr){
final int n=arr.length;
final int SquaredN=n*n;//square of n
final int M=(n*n*(n*n+1)/2)/n;
int RowSum=0, ColumnSum=0, PrimaryDiagonalSum=0, SecondaryDiagonalSum=0;//store row sum,solumn sum etc

boolean[] vis= new boolean[n*n];//to mark elements
for(int row=0; row<n; row++){
RowSum=0;
ColumnSum=0;
for(int col=0; col<n; col++)
{
if(arr[row][col]<1 || arr[row][col]>SquaredN) return false;
if(vis[arr[row][col]-1]==true) return false;
vis[arr[row][col]-1]=true;
RowSum += arr[row][col];
ColumnSum += arr[col][row];
}
PrimaryDiagonalSum += arr[row][row];
SecondaryDiagonalSum += arr[row][n-row-1];
if(RowSum!=M || ColumnSum!=M) return false;//if row sum and column sum not equal to M return false
}
if(PrimaryDiagonalSum!=M || SecondaryDiagonalSum!=M) return false;//if diagonal sum not equal to M return false
return true;
}
public static void main(String []args){
   System.out.println("Enter n");
   Scanner sc=new Scanner(System.in);
   int n=sc.nextInt();


int[][] a=new int[n][n];
System.out.println("Enter Matrix");

for (int i=0;i<n ;i++ )
{
   for (int j=0;j<n ;j++ )
   {
       a[i][j]=sc.nextInt();
   }
  
}
System.out.println(isMagicSquare(a));
}
}

========================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ javac Magic.java
akshay@akshay-Inspiron-3537:~/Chegg$ java Magic
Enter n
3
Enter Matrix
8 1 6
3 5 7
4 9 2
true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote