Answered step by step
Verified Expert Solution
Question
1 Approved Answer
solve only Q1: part B Assignment 1. Any nxn magic square (where n is an odd integer) consists of an nxn matrix whose elements contain
solve only Q1: part B
Assignment 1. Any nxn magic square (where n is an odd integer) consists of an nxn matrix whose elements contain the numbers n(n+1) 1, 2, 3,..., n such that the sum of each row, column and diagonal is equal to For example, the following 2 3(32+1) magic square for n=3, with the sum of each row, column and diagonal being = 15: 618 75 3 29 4 (a) An algorithm for generating an n x n magic square for odd n is as follows: NOTE: Assume the rows and columns wrap around (i.e., moving one column left from the first column gives the last column) Create a 2 dimensional array of size n by n and set all values to be 0 Set x = 1, y = 171 (row 1 and column n?). Insert 1 at x, y for i = 2 to na do if element 1 1,7 1 is empty i.e. = 0) then x = x - 1, y = y - 1 else x= x + 1, y = y end if Insert i at 1, y end for Write a command line application that prompts the user for an odd integer and displays a magic square of that size to standard output (i.e. the command line). (b) Write a command line game with the following functionality: The application should prompt the user for an odd integer and create a magic square of that size. The magic square should then be shuffled by repeatedly (for n times) choosing a random element and swapping it with a random neighbour (not including diagonals). The shuffled square should be displayed to the user, who must attempt to reconstruct a magic square. The user makes moves by giving input of the form: ij direction where i and j specify the row and column of an element to be swapped, and direction (either U, D L ,R representing up, down, left and right) specifies which direction it should be swapped with. For example, the move 2 1 D applied to the square above would give: 6 1 2 5 8 3 4 7 9 On completion, the game should report the number of moves made. (18 Marks)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started