subject

There is a simple method for constructing a magic square for any odd value of n:

Make a blank n by n grid. We need to populate it with the integers from 1 to n2.

Set i to 1.

Set the current grid position to the location in the middle of the top row.

Insert the number i into the current grid position.

If i is equal to n2, we're done.

If i is a multiple of n, move the current grid position down one (i. e., in the direction of "south" on a map). Otherwise, move the current grid position diagonally up and right (i. e., in a "northeast" direction on a map), wrapping to the first column or last row if the move leads outside the grid.

Increment i by 1.

Return to step 4.

In this assignment, you will create a class of MagicSquare objects, and print out magic squares with sides 1, 3, 5, 7, 9, 11, and 13.

The class MagicSquare should have two properties:

sideLength, an integer representing how many numbers are in each row and column of the magic square.

grid, a two-dimensional list of size sideLength by sideLength representing the contents of the magic square.

The class MagicSquare should have two methods:

__init__(), which takes as an argument an integer side representing the value for the new object's sideLength. __init__ should create the two-dimensional list grid and initially populate it with zeroes. __init__ should then use the algorithm described above to replace the zeroes in grid with the integers from 1 to sides2.

display(), which prints out a MagicSquare object. It should print out each number in a field of size 5, right-justified, similar to the example above. It should also print a blank line after the magic square.

Create a main program that uses a loop to print the seven magic squares listed above, each preceded by the title, "Magic Square of size ", followed by the corresponding value for side. Leave a blank line between the title and the magic square. See the example above.

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 20:40
Assume that there is a 4% rate of disk drive failure in a year. a. if all your computer data is stored on a hard disk drive with a copy stored on a second hard disk drive, what is the probability that during a year, you can avoid catastrophe with at least one working drive? b. if copies of all your computer data are stored on three independent hard disk drives, what is the probability that during a year, you can avoid catastrophe with at least one working drive?
Answers: 1
question
Computers and Technology, 24.06.2019 05:30
Why is hard disk space important to an audio engineer? why are usb ports and firewire ports useful for an audio engineer? explain in 2-3 sentences. (3.0 points) here's a list of different audio software: ableton live apple inc.'s garageband apple inc.'s logic studio digidesign's pro tools propellerhead sofware's reason sony creative software's acid pro steinberg cubase steinberg nuendo choose one of the software programs listed above, and then go to that software program's web site. read about what the software program is used for, and then write 4-5 sentences about what you learned. (10.0 points) which type of software license is the most limiting? why? explain in 2-3 sentences. (3.0 points) when sending a midi channel voice message, how can you control the volume of the sound? explain in 2-3 sentences. (4.0 points)
Answers: 1
question
Computers and Technology, 24.06.2019 11:30
Convert 11001110(acdd notation) into decimal
Answers: 2
question
Computers and Technology, 24.06.2019 21:30
Write an algorithm to check if a number is even or odd and show with flow chart step by step
Answers: 2
You know the right answer?
There is a simple method for constructing a magic square for any odd value of n:

Make a...
Questions
question
Mathematics, 13.11.2020 01:00
question
History, 13.11.2020 01:00
question
Mathematics, 13.11.2020 01:00
question
Mathematics, 13.11.2020 01:00
Questions on the website: 13722359