In: Computer Science
write a java programming using 2D arrey.
The Lo Shu Magic Square is a grid with 3 rows and 3 columns. The Lo
Shu Magic Square has the following properties:
The grid contains the numbers 1 through 9 (each number only once)
The sum of each row, each column and each diagonal are the same
In a program you can simulate a magic square using a two-dimensional array. Write a method that accepts a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square. Test the function in a program.
Detailed Description:
In main create two arrays:
// Create a magic two-dimensional array.
int[][] magicArray = { {4, 9, 2},
{3, 5, 7},
{8, 1, 6} };
// Create a normal two-dimensional array.
int[][] normalArray = { {1, 2, 3},
{4, 5, 6},
{7, 8, 8} };
For the normalArray call showArray and then showResult. Then do the same for magicArray
showArray accepts a two-dimensional array and prints it row by row
showResult call isMagicSquare with the two-dimensional array reference as the parameter. It prints a message based on the return value which is Boolean.
The isMagicSquare method accepts a two-dimensional int array as an argument, and returns true if the array meets all the requirements of a magic square. Otherwise it returns false. You can create separate methods to checkRange (all must be from 1-9), checkUnique (see if all values are unique), checkRowSum, checkColSum, checkDiagSum. If each of these 3 return true then it is a Magic Square.
Output should be
1 2 3
4 5 6
7 8 8
This is not a Lo Shu magic square.
4 9 2
3 5 7
8 1 6
This is a Lo Shu magic square.
public class CheckMagicSquare {
private static boolean isMagicSquare(int[][] magicArray)
{
// calculate the sum of
// the prime diagonal
int sum = 0,sum2=0;
int N = magicArray.length;
for (int i = 0; i < N; i++)
sum = sum + magicArray[i][i];
// the secondary diagonal
for (int i = 0; i < N; i++)
sum2 = sum2 + magicArray[i][N-1-i];
if(sum!=sum2)
return false;
// For sums of Rows
for (int i = 0; i < N; i++) {
int rowSum = 0;
for (int j = 0; j < N; j++)
rowSum += magicArray[i][j];
// check if every row sum is
// equal to prime diagonal sum
if (rowSum != sum)
return false;
}
// For sums of Columns
for (int i = 0; i < N; i++) {
int colSum = 0;
for (int j = 0; j < N; j++)
colSum += magicArray[j][i];
// check if every column sum is
// equal to prime diagonal sum
if (sum != colSum)
return false;
}
return true;
}
public static void main(String[] args) {
int[][] magicArray = { {4, 9, 2},
{3, 5, 7},
{8, 1, 6} };
int[][] normalArray = { {1, 2, 3},
{4, 5, 6},
{7, 8, 8} };
if( isMagicSquare(normalArray)){
for (int i = 0; i < normalArray.length; i++) {
for (int j = 0; j < normalArray.length; j++) {
System.out.print(normalArray[i][j]+" ");
}
System.out.println("");
}
System.out.println("This is a Lo Shu magic square.");
}else{
for (int i = 0; i < normalArray.length; i++) {
for (int j = 0; j < normalArray.length; j++) {
System.out.print(normalArray[i][j]+" ");
}
System.out.println("");
}
System.out.println("This is not a Lo Shu magic square.");
}
if (isMagicSquare(magicArray)){
for (int i = 0; i < magicArray.length; i++) {
for (int j = 0; j < magicArray.length; j++) {
System.out.print(magicArray[i][j]+" ");
}
System.out.println("");
}
System.out.println("This is a Lo Shu magic square.");
}else{
for (int i = 0; i < magicArray.length; i++) {
for (int j = 0; j < magicArray.length; j++) {
System.out.print(magicArray[i][j]+" ");
}
System.out.println("");
}
System.out.println("This is a Lo Shu magic square.");
}
}
}
/* PLEASE UPVOTE */