Question: this is for java. 1. Any nxn magic square (where n is an odd integer) consists of an nxn matrix whose elements contain the numbers

 this is for java. 1. Any nxn magic square (where n

this is for java.

1. Any nxn magic square (where n is an odd integer) consists of an nxn matrix whose elements contain the numbers 1,2,3,..., n such that the sum of each row, column and diagonal is equal to n(n+1). For example, the following magic square for n = 3, with the sum of each row, column and diagonal being 3(3+1) = 15: 61 8 7 5 3 2 94 (a) An algorithm for generating an n x n magic square for odd n is as follows: NOTE: Assume the rows and columns wrap around (i.e., moving one column left from the first column gives the last column) Create a 2 dimensional array of size n by n and set all values to be 0 Set 1 = 1, y = (row 1 and column "+). Insert 1 at r, y for i = 2 to na do if element 1-1,4- 1 is empty (i.e. = 0) then =: -1, y = y-1 else r=r+1, y = y end if Insert i at 1, y end for Write a command line application that prompts the user for an odd integer and displays a magic square of that size to standard output (i.e. the command line). [HINTS: Recall that Java arrays start at the element 0; it may help to define a class to store a square matrix). (12 Marks)

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!