Question: IN JAVA Download the below program and execute. public class MagicSquare { public static void main (String[] argv) { // Some test cases. int[][] square

IN JAVA

Download the below program and execute.

public class MagicSquare { public static void main (String[] argv) { // Some test cases. int[][] square = generateSquare (3); print (square); square = generateSquare (5); print (square); } // Current row/column and next row/column. static int row, col; static int nextRow, nextCol; static int[][] generateSquare (int size) { // This algorithm only works for odd-sizes. if (size % 2 != 1) { System.out.println ("size must be odd"); System.exit (0); } int[][] A = new int [size][size]; // Start with middle in top row. row = 0; col = size/2; A[row][col] = 1; for (int n=2; n<=size*size; n++) { // Go up diagonally to the right. computeNext (size); if (A[nextRow][nextCol] == 0) { // Place next number here if unoccupied. A[nextRow][nextCol] = n; } else { // Else, place directly below current number. nextRow = row + 1; nextCol = col; A[nextRow][nextCol] = n; } // Update. row = nextRow; col = nextCol; } //end-for return A; } static void computeNext (int size) { if (row == 0) { // If we're at the top, next row wraps around. nextRow = size - 1; } else { // Otherwise, go to previous row. nextRow = row - 1; } if (col == size-1) { // If we're at the rightmost col, wrap around to leftmost. nextCol = 0; } else { // Otherwise, next column is the one to the right. nextCol = col + 1; } } static void print (int[][] A) { System.out.println ("Square of size " + A.length + ":"); for (int i=0; iTO DO: THEN modify the print() method so that it prints out row and column sum as follows:

Square of size 3:

8 1 6 I 15

3 5 7 I 15

4 9 2 I 15

-----------

15 15 15

Square of size 5:

17 24 1 8 15 I 65

23 5 7 14 16 I 65

4 6 13 20 22 I 65

10 12 19 21 3 I 65

11 18 25 2 9 I 65

-- -- -- -- -- -- -- -- --

65 65 65 65 65

fyi the lines in bold are not numbers its just showing the matrix

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!