You are on page 1of 2

CPSC101 (Computer Programming II) January 2014 HW #2 Due: February 14 (in class)

Max Points: 65
General instructions: Provide a printout of an unedited script file for the programming problems. All code must be properly documented and conform to the style outlined in your text (indentations, etc). All input is expected to be from keyboard unless otherwise specified. All output must be properly formatted. Testing must be thorough. Programs should be user-friendly.

1. (Maze Traversal using Recursive Backtracking) The grid of #s and dots (.) shown below is a two-dimensional array representation of a maze. The #s represent the walls of the maze, and the dots represent locations in the possible paths through the maze. Moves can be made only to a location in the array that contains a dot. Write a recursive method (mazeTraversal) to walk through mazes like this one. The method should receive as arguments a 12-by-12 character array representing the maze and the current location in the maze (the first time this method is called, the current location should be the entry point of the maze). As mazeTraversal attempts to locate the exit, it should place the character x in each square in the path. There is a simple algorithm for walking through a maze that guarantees finding the exit (assuming there is an exit). If there is no exit, you will arrive at the starting location again. The algorithm is as follows: From the current location in the maze, try to move one space in any of the possible directions (down, right, up or left). If it is possible to move in at least one direction, call mazeTraversal recursively, passing the new spot on the maze as the current spot. If it is not possible to go in any direction, back up to a previous location in the maze and try a new direction for that location. Program the method to display the maze after each move so the user can watch as the maze is solved. The final output of the maze should display only the path needed to solve the mazeif going in a particular direction results in a dead end, the xs going in that direction should not be displayed. [Hint: To display only the final path, it may be helpful to mark off spots that result in a dead end with another character (such as '0').] # # . # # # # # # # # # # . . # . # . # . # . # # . # # . # . . . # . # # . . . . # # # . # . # # # # # . . . . . # . # # . . . # # # # . # . # # . # . # . . . . . . # # . # . # # # # . # # # # . # . . . . . . # . # # . # # # # # # # # . # # . . . . . . . . . . # # # # # . # # # # # # #

(30 points)

2. A Fibonacci sequence is of the form 1, 1, 2, 3, 5, 8, 13, 21, 34, where each number, except the first two, is the sum of preceding two numbers. For example, Fib(1) = 1, Fib(2) = 1, Fib(6) = 8, and so forth. a) Both recursive and iterative methods to compute the Nth Fibonacci number are provided in your text and also widely available on the Internet. Use these methods to test various Fibonacci numbers. Specifically, print the values for N = 25, N = 30, and N=40. b) Include an instance variable to count the number of calls made to the recursive method. Also, print the computation time (milliseconds) used to calculate the Nth Fibonacci number. Compare this running time with that of the iterative implementation. c) Exercise E13.20 in your text. The recursive computation of Fibonacci numbers can be sped up significantly by keeping track of the values that have already been computed. Provide an implementation of the fib method that uses this strategy. Whenever you return a new value, also store it in an auxiliary array. However, before embarking on a computation, consult the array to find whether the result has already been computed. Compare the running time of your improved implementation with that of the original recursive implementation and the iterative implementation. (10+10+15 points)

You might also like