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 21:00
Which of these is most responsible for differences between the twentieth century to the twenty-first century?
Answers: 2
question
Computers and Technology, 23.06.2019 13:30
Anetwork security application that prevents access between a private and trusted network and other untrusted networks
Answers: 1
question
Computers and Technology, 24.06.2019 03:00
Will do anything for brainlest so can you guys me out i will try my best to you out
Answers: 1
question
Computers and Technology, 24.06.2019 04:30
Fall protection, confined space entry procedures, controlled noise levels, and protection from chemical hazards are some of the things that contribute to a safe and
Answers: 1
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
Mathematics, 10.11.2020 19:40
question
Mathematics, 10.11.2020 19:40
question
Mathematics, 10.11.2020 19:40
question
Mathematics, 10.11.2020 19:40
question
History, 10.11.2020 19:40
question
Mathematics, 10.11.2020 19:40
question
History, 10.11.2020 19:40
Questions on the website: 13722361