Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n2 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. 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 Write a program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4 × 4 array. You need to test two features: Does each of the numbers 1, 2, ..., 16 occur in the user input? When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other? Implement the following algorithm to construct magic n × n squares; it works only if n is odd. Set row = n - 1, column = n / 2. For k = 1 ... n * n Place k at [row][column]. Increment row and column. If the row or column is n, replace it with 0. If the element at [row][column] has already been filled Set row and column to their previous values. Decrement row. Here is the 5 × 5 square that you get if you follow this method: 11 18 25 2 9 10 12 19 21 3 4 6 13 20 22 25 5 7 14 15 17 24 1 8 15 Write a program whose input is the number n and whose output is the magic square of order n if n is odd. Write a program that reads a sequence of input values and displays a bar chart of the values, using asterisks, like this:
Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n2 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. 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 Write a program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4 × 4 array. You need to test two features: Does each of the numbers 1, 2, ..., 16 occur in the user input? When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other? Implement the following algorithm to construct magic n × n squares; it works only if n is odd. Set row = n - 1, column = n / 2. For k = 1 ... n * n Place k at [row][column]. Increment row and column. If the row or column is n, replace it with 0. If the element at [row][column] has already been filled Set row and column to their previous values. Decrement row. Here is the 5 × 5 square that you get if you follow this method: 11 18 25 2 9 10 12 19 21 3 4 6 13 20 22 25 5 7 14 15 17 24 1 8 15 Write a program whose input is the number n and whose output is the magic square of order n if n is odd. Write a program that reads a sequence of input values and displays a bar chart of the values, using asterisks, like this: