[1] Objectives: The main purpose of this assignment is to make sure you are familiar with using arrays (in particular, two dimensional arrays). In addition, it is important that you know how to use functions to structure your program. [2] Description: We are going to simulate a random walk by using the random number generator that we have been using for a while by now. The walk is taken place on a 2 dimensional board of size n x n where n =15 is a fixed constant. You may want to test your program starting with a smaller n. Here are some rules. • You start out at the center of the board. You should memorize your current position. • You are allowed to walk in one of the four directions (1-E, 2-W, 3-N, and 4-S) from your current position. • The walk is totally memory-independent, so you repeat the process again from the current position. • The user is asked interactively on the number of steps to walk starting from your current position. A zero indicates termination of the walk. • You are not allowed to walk off the board ("hit the wall"). If that happens, the walk is over. • You should show every step (the coordinate of each action) of your walk so that we can know them whole path. Suppose we have a good random number generator, our current position should hover around the starting position. The chance of hitting the "wall" is small but nonzero. The probability decreases as the size in-creases. Does the simulation agree with this prediction? We shall keep a counter for each location of the board to store the number of times the location was “visited.” The number is increased by 1 every time you step on that location. [3] Input: A seed for the random number generator once, and the number of steps to walk (repeat). [4] Output: Every time before you ask for another input of number of steps, you want to inform the user the current position of you and the counters. Two examples are shown below. Random Walk Simulation Seed: 800 Number of steps (0 to stop): 1 Coordinate: (6, 5) Total Steps: 1 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ....X...... 0 0 0 0 1 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 Number of steps (0 to stop): 20 Coordinate: (7, 5) Coordinate: (7, 4) Coordinate: (8, 4) Coordinate: (8, 5) Coordinate: (7, 5) Coordinate: (7, 6) Coordinate: (6, 6) Coordinate: (6, 7) Coordinate: (6, 8) Coordinate: (7, 8) Coordinate: (7, 9) Coordinate: (7, 8) Coordinate: (6, 8) Coordinate: (6, 7) Coordinate: (7, 7) Coordinate: (8, 7) Coordinate: (9, 7) Coordinate: (10, 7) Total Steps: 21 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 1 1 2 2 0 0 0 ........... 0 0 0 1 2 1 1 3 2 0 0 ........... 0 0 0 1 1 0 1 0 0 0 0 ........... 0 0 0 0 0 0 1 0 0 0 0 ......X.... 0 0 0 0 0 0 1 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 Number of steps (0 to stop): 40 Coordinate: (9, 7) Coordinate: (8, 7) Coordinate: (9, 7) Coordinate: (8, 7) Coordinate: (9, 7) Coordinate: (9, 6) Coordinate: (10, 6) Coordinate: (10, 5) Coordinate: (10, 6) Coordinate: (10, 7) Coordinate: (9, 7) Coordinate: (9, 6) Coordinate: (9, 7) Coordinate: (9, 8) Coordinate: (10, 8) Coordinate: (10, 9) Coordinate: (11, 9) Total Steps: 38 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 0 0 0 0 0 0 0 ........... 0 0 0 0 1 1 2 2 0 0 0 ........... 0 0 0 1 2 1 1 3 2 0 0 ........... 0 0 0 1 1 0 3 0 0 0 0 ........... 0 0 0 0 0 2 6 1 0 0 0 ........... 0 0 0 0 1 2 2 1 1 0 0 ........X.. 0 0 0 0 0 0 0 0 1 0 0 The Random Walk is Over in 38 steps. Press any key to continue . . . |
|
|