subject

An x n matrix that is filled with the numbers 1, 2, 3, n² is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
implement the following algorithm to construct the magic n-by-n squares. this algorithm works only if n is odd.
place a 1 in the middle of the bottom row.
after k has been placed in the (i, j) square, place k+1 into the square to the right and down, wrapping around the borders.
however, if the square to the right and down has already been filled, or if you are in the lower right corner, then you must move to the square straight up (from the last square that you were on) instead.
skeleton:
# populate a 2-d list with numbers from 1 to n2
def make_square ( n ):
# print the magic square in a neat format where the numbers
# are right justified
def print_square ( magic_square ):
# check that the 2-d list generated is indeed a magic square
def check_square ( magic_square ):
def main():
# prompt the user to enter an odd number 3 or greater
# check the user input
# create the magic square
# print the magic square
# verify that it is a magic square
main()
in your function main() you will prompt the user to enter an odd number. you must check that the input is a positive odd number greater than or equal to 3. if it is not, you will prompt the user to re-enter the number and check again and again.
then you will create a 2-d list representing the magic square. you will then print out the magic square in a neat format by calling the function in the function print_square() you must use print with formatting.
you will then call the function this function checks that the sum of all the rows have the same value and prints out that sum. it checks that the sum of all the columns have the same value and prints out that sum. it sums the two main diagonals and prints out the sum. for a magic square of size n, the sum is n * (n2 + 1) / 2.
this is a sample of what the program will output:
enter an odd number: 5
here is a 5 x 5 magic square:
11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15
sum of row = 65
sum of column = 65
sum diagonal (ul to lr) = 65
sum diagonal (ur to ll) = 65

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 17:00
Match the following. 1. show grouping of word processing tasks that can be performed quick access toolbar 2. shortcut location for commonly used elements scroll bars 3. organized commands used to modify documents ribbon 4. used to align and measure content in a word screen zoom bar 5. vertical and horizontal bars that are used to navigate through a document contextual tabs 6. displays the name of the document in use ruler 7. allows users to enlarge or shrink a visual of a word document title bar
Answers: 2
question
Computers and Technology, 22.06.2019 19:50
Write a car class having two private member variables called tank and speed. write public methods called pumpgas and gofast. the method pumpgas gets an integer for gas that must be pumped. that value needs to be added to tank (no more than 20 gallons). it must return the amount of gas that is purchased ($4 per gallon). the method gofast should increase the speed by 5 each time it is called.write a constructor for the above class that initialized both variables to zero.write a tostring to display both the tank and speed when the car is printed.modify the car class to implement the interface comparable and an interface called carinter having the public methods in carinter.write the main program to create an array of size 5 of type car. create 5 car objects having each location of the array to refer to one of the cars. test the pumpgas, gofast, equals method on the array items. write an enhanced loop to print all the car values (using a tostring written last time).write a generic method to find the minimum of four items. pass int, double, char, string and car objects to test this method.
Answers: 1
question
Computers and Technology, 23.06.2019 07:00
To produce a starlight effect in her photograph, lina should choose the filter for her camera.
Answers: 1
question
Computers and Technology, 23.06.2019 19:30
2. fluorine and chlorine molecules are blamed fora trapping the sun's energyob forming acid rainoc producing smogod destroying ozone molecules
Answers: 2
You know the right answer?
An x n matrix that is filled with the numbers 1, 2, 3, n² is a magic square if the sum of the eleme...
Questions
question
English, 30.10.2020 16:50
question
Mathematics, 30.10.2020 16:50
question
Mathematics, 30.10.2020 16:50
question
History, 30.10.2020 16:50
question
Mathematics, 30.10.2020 16:50
Questions on the website: 13722363