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, 21.06.2019 23:30
Me2540 week 5 assignment what do i want to know?
Answers: 1
question
Computers and Technology, 22.06.2019 16:30
Which of the following statements best describes it careers?
Answers: 2
question
Computers and Technology, 23.06.2019 06:00
Which statement is true of web-based social media? a.they allow consumers to interact with and update content. b.they cannot be updated easily, as compared to print media. c.they are expensive to produce and maintain, as compared to print and television. d.they can exist independent of the internet.
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
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, 09.05.2021 23:30
question
Health, 09.05.2021 23:30
Questions on the website: 13722363