Search the whole station

数学应用编程代写 Math 124代写 数学编程代写 数学作业代写

Math 124 – Programming forMathematical Applications

Project 1 – The Trapped Knight

数学应用编程代写 Description In this project, you will write a computer code to generate a particular sequence of numbers described in the following YouTube video:

Description

In this project, you will write a computer code to generate a particular sequence of numbers described in the following YouTube video: The Trapped Knight (https://www.youtube.com/watch?v=RGQe8waGJ4w)

Begin by watching the video and make sure you understand exactly how the sequence is generated. Then continue to implement the code in the 3 parts described below.

Part 1 – Initialize the board 数学应用编程代写

We will store the chess board in a 2d-array of integers. The size of the board is -by- , for a given integer . This means the board extends from the center square by steps in all directions.

The first step is to initialize the board by filling it with the integers described in the video. Finish the implementation of the function definition in the cell below such that it returns this “spiral pattern” for any given input parameter .

An example is given below: for the following input

board = initialize_board(3)

the correct output is

7×7 Matrix{Int64}:
37 36 35 34 33 32 31
38 17 16 15 14 13 30
39 18 5 4 3 12 29
40 19 6 1 2 11 28
41 20 7 8 9 10 27
42 21 22 23 24 25 26
43 44 45 46 47 48 49

Test your function for various values of to make sure it is correct before you continue.

Hints:

Note that since Julia uses 1-based indexing, the center square of the array board is given by element

board[n+1,n+1] .

After the center has been placed, there are exactly “circles” of numbers of increasing radius. This is naturally implemented using a for-loop.

In each “circle”, there are 4 segments going up, left, down, and right. These are also naturally implemented using a sequence of 4 for-loops.

In [1]:

# Initializes the board on a [-n:n]x[-n:n] domain with spiral numbers
#
# Example: initialize_board(2) returns
# 17 16 15 14 13
# 18 5 4 3 12
# 19 6 1 2 11
# 20 7 8 9 10
# 21 22 23 24 25
#
# Inputs:
# n = integer size of board to allocate
# Outputs:
# board = 2n+1 x 2n+1 integer array filled with spiral numbers
function initialize_board(n)

end

Out[1]:

initialize_board (generic function with 1 method)

数学应用编程代写
数学应用编程代写

Part 2 – Simulate the walk 数学应用编程代写

Next we will write the function to simulate the walk and produce the sequence. This function will take an initialized board as input, and produce a list of numbers as well as the corresponding x- and y-coordinates.

For example, the following input:

board = initialize_board(2)
display(board)
seq, xs, ys = simulate_walk(board);
println("Sequence = ", seq)
println("x-coordinates = ", xs)
println("y-coordinates = ", ys)

should produce the following correct output:

5×5 Matrix{Int64}:
17 16 15 14 13
18 5 4 3 12
19 6 1 2 11
20 7 8 9 10
21 22 23 24 25
Sequence = [1, 10, 3, 6, 9, 4, 7, 2, 5, 8, 11, 14]
x-coordinates = [0, 2, 1, -1, 1, 0, -1, 1, -1, 0, 2, 1]
y-coordinates = [0, 1, -1, 0, 1, -1, 1, 0, -1, 1, 0, -2]

Again test your code, first using small values of as shown above, which makes it easier to look at the results and find errors.

Hints:

It is convenient to create another 2d-array of booleans, indicating if a square has been visited or not.

Make sure you never allow the knight to jump outside the board. That is, the only valid positions are steps from the center square in either direction

In [2]:

# Simulates the trapped knight walk on a pre-initialized board and returns information about knight
#
# Inputs:
# board = 2n+1 x 2n+1 integer array filled with spiral numbers
# Outputs:
# sequence = integer array containing the sequence of spiral numbers the knight jumped to during wa
# x_path = integer array containing the x coordinates of each step of knight walk
# y_path = integer array containing the y coordinates of each step of knight walk
function simulate_walk(board)

end

Out[2]:

simulate_walk (generic function with 1 method)

Part 3 – Generate the full sequence and plot the path 数学应用编程代写

Finally, use your code to generate the full sequence ( is sufficient), output the last number, and plot the path by straight lines between all the visited x,y-coordinates.

In [ ]:

In [ ]:

数学应用编程代写
数学应用编程代写

更多代写:CS作业代写网站  托福代考多少钱  英国Budgeting代考  怎么写summary  怎么写conclusion  计算机论文写作技巧

合作平台:essay代写 论文代写 写手招聘 英国留学生代写

The prev: The next:

Related recommendations

1
您有新消息,点击联系!