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

Lo shu magic square—Write a program that satisfies following requires: 1. Fill w

ID: 3724271 • Letter: L

Question

Lo shu magic square—Write a program that satisfies following requires:
1. Fill with random numbers 1-9
2. The number 15 should not be in the program
3. Run until you find a solution and then display it.

Lo shu magic square—Write a program that satisfies following requires:
1. Fill with random numbers 1-9
2. The number 15 should not be in the program
3. Run until you find a solution and then display it.

Lo shu magic square—Write a program that satisfies following requires:
1. Fill with random numbers 1-9
2. The number 15 should not be in the program
3. Run until you find a solution and then display it. Lo shu magic square—Write a program that satisfies following requires:
1. Fill with random numbers 1-9
2. The number 15 should not be in the program
3. Run until you find a solution and then display it.

8. Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figur The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. . The sum of each row, each column, and each diagonal all add up to the same num- ber. This is shown in Figure 7-20. In a program, you can simulate a magic square using a two-dimensional array. Write a n 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. Figure 7-19 Lo Shu Magic Square 4 2 3 5 Figure 7-20 Sums of the rows, columns, and diagonals 15 4 9 2 15 3 5 715 6 15 15 15 15 15

Explanation / Answer

public class MagicSquare
{
public static boolean check(int[][] m)
{
int sum,s;
sum=0;
for (int j = 0; j < 3; j++) //first set the sum 15
sum = sum + m[0][j];
  
//Check rows
s = 0;
for (int i = 1; i < 3; i++)
{ // i and j are rows and columns respectively
s = 0;
for (int j = 0; j< 3; j++)
s = s + m[i][j];
if (s != sum)
return false;
}

//check columns
for (int j = 0; j< 3; j++)
{
s = 0;
for (int i = 0; i < 3; i++)
{
s = s + m[i][j];
}
if (s != sum)
return false;
}

//check diagonals
s =0;
for (int i = 0; i < 3; i++)
{
s = s+ m[i][i];
}
if (s != sum)
return false;

s =0;
for (int i = 0; i < 3; i++)
{
s = s + m[i][2 - i];
}
if (s != sum)
return false;

//check that the numbers 1..9 appear
boolean[] appears = new boolean[9];
for (int i=0; i < 9; i++)
{
appears[i] = false;
}

//Check each one
for (int j = 0; j< 3; j++)
{
for (int i = 0; i < 3; i++)
{
int num = m[i][j];
if (appears[num-1])
return false;
appears[num-1] = true;
}
} // since there are 9 positions in both appears and a[][], then if we get to this point
// then all the values in appears[] must be true
return true;
  
}

// main function to test the MagicSquare function

public static void main (String[] args)
{
int[][] matrix = {{4,9,2},{3,5,7},{8,1,6}}; // Change the values of this array to check for other matrix
System.out.println(MagicSquare.check(matrix));
}
}

// The output obtained will be true because I have checked for a matrix which is a Lo Shu Magic Square.

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