Question

In: Computer Science

The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in...

The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure 8-23. The Lo Shu Magic Square has the following properties:

l The grid contains the numbers 1 through 9 exactly.

l The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure 8-24.

In a program, you can simulate a magic square using a two-dimensional array. Design a program that initializes a two-dimensional array with values entered by the user. The program should determine whether the array is a Lo Shu Magic Square.

Solutions

Expert Solution

Copyable Code:

MagicSquare.java:

public class MagicSquare

{

//function definition for check magic square

public static boolean checkMagicSquare(int[][] arr)

{

//declare the variable r for row, c for column

//declare the variable for sum

int sum=0,s, r, c,i;

for (c = 0; c<3; c++)

//assign the sum is 15 which is in two dimension array

sum += arr[0][c];

//check two rows

s = 0;

for (r = 1; r < 3; r++)

{

s = 0;

for (c = 0; c< 3; c++)

s += arr[r][c];

if (s != sum)

return false;

}

//validate columns

for (c = 0; c< 3; c++)

{

s = 0;

for (r = 0; r < 3; r++)

{

s += arr[r][c];

  

}

if (s != sum)

return false;

  

}

//validate diagonal 1

s =0;

for (r = 0; r < 3; r++)

{

s += arr[r][r];

}

if (s != sum)

return false;

//validate diagonal 2

s =0;

for (r = 0; r < 3; r++)

{

s += arr[r][2 - r];

  

}

if (s != sum)

return false;

//check that the numbers 1 to 9 existing

//assign the variable for selected

boolean[] selected = new boolean[9];

for (i=0; i < 9; i++)

{

selected[i] = false;

}

//check every number

for (c = 0; c< 3; c++)

{

for (r = 0; r < 3; r++)

{

int num = arr[r][c];

if (selected[num-1])

return false;

selected[num-1] = true;

  

}

}

//here check the every values in appears are true but

// since there are 9 positions in both selected and arr[][]

return true;

}

public static void main (String[] args)

{

int[][] sampleArray1 = {{4,9,2},{3,5,7},{8,1,6}};

System.out.println("Sample array 1 Lo Shu Magic Square: "+MagicSquare.checkMagicSquare(sampleArray1));

int[][] sampleArray2 = {{4,9,2},{3,1,5},{8,7,6}};

System.out.println("Sample array 2 Lo Shu Magic Square: "+MagicSquare.checkMagicSquare(sampleArray2));

}

}


Related Solutions

The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. The Lo Shu Magic Square has the following properties:
Python programmingThe Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. 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 number as shown in figure.In a program you can simulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figures below. 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 number. This is shown in Figure B. In a program you can stimulate a magic square using a two-dimensional list. Write a function...
1 How can we specify the number of rows and columns in a grid layout? 2....
1 How can we specify the number of rows and columns in a grid layout? 2. What happens when we don't have enough items to be displayed in a grid layout with a specified size? 3. How can we specify a grid layout component to occupy more than one columns or rows? 4. What happens if the components in a linear and grid layout exceed the layout size? How can we handle this problem so that all components can be...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result: 25 69 51 26 171 68 35...
* (create grid of images any images) apply a CSS3 grid, so that several columns and...
* (create grid of images any images) apply a CSS3 grid, so that several columns and rows of images now appear. Apply a color to the background and font to each grid box. * In each box, add two buttons via JQuery or createElement(). One button will read 'make favorite' and the other will read 'unfavorite.' * If the 'make favorite' button in any box is clicked, in the grid page, save the image's url or filename to a cookie....
JavaScript Find and Replace Text Main text: a text area with 3 rows and 20 columns....
JavaScript Find and Replace Text Main text: a text area with 3 rows and 20 columns. The default text is “Hello testing”. The text area cannot be empty. • Find text: a text box for the user to key in text he/she wants to find. • Replacement text: a text box for the user to key in the replacement text. If the find text is empty, this element should be disabled (i.e. user cannot key in anything here). • Find...
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given).
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given). Important: Next, the expression "a single line of code" implies a single command or equality. In other words, the code: X=A+1; X=X+B; is considered to be TWO lines of code, even though it can be written as one line. (a) (3%) Write a single line of code which saves the first two rows of array A...
/*Question 3: The following data contains five columns (variables) and five rows (observations). First, read the...
/*Question 3: The following data contains five columns (variables) and five rows (observations). First, read the data into SAS to create a data set. Notice that the first, third, and the fifth variable have missing values. Please replace the missing values of the first, third, and fifth variable with 30, 40, and 50, respectively. Next, for all the variables, if a value is at least 100, make an adjustment to the value such that its new value is equal to...
Consider the following game. Ann chooses rows, and Bob chooses columns. [12 marks] LMR 3 4...
Consider the following game. Ann chooses rows, and Bob chooses columns. [12 marks] LMR 3 4 2 0 1 3 3 2 4 4 4 0 0 2 1 1 0 1 U C D (a) Find all pure strategy Nash Equilibria. (b) Find the set of Rationalizable strategies for each player. Find a dominating strategy for each deletion. (c) Is there a Nash equilibrium in which Bob puts strictly positive probability on L and M but none on R?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT