CS 2500 Programming Assignment #5 Number Of Paths - Comparing Two Solutions ================================================== DUE DATES: 1. Level 1 version of the program, including all the test driver code, header comments for the main program and the two NumPaths functions, and stubs for the two versions of NumPaths. DUE: Wednesday, December 3, 1997 2. (a) Finished version of the program, including the code for the two versions of NumPaths. (b) The answers to questions (1) and (2). (c) The filled-in chart. DUE: Monday, December 8, 1997 (Read this assignment sheet for more description of what to turn in.) ================================================== THE ASSIGNMENT In this assignment you will write and compare two procedures that count the number of paths possible to move in a 2-dimensionsal square grid from row 1, column 1 to row N, column N. Steps are restricted to going up or to the right. Diagonal steps, steps down, and steps to the left are not allowed. The illustration below shows 3 such paths if N = 9. The start and finish are labelled S and F, respectively, and the three paths, excluding their start and finish, are labelled with X's, Y's, and Z's. --- --- --- --- --- --- --- --- --- | X | X | X | X | X | X | X | X | F | 9 --- --- --- --- --- --- --- --- --- | X | | | | | | | Z |ZY | 8 --- --- --- --- --- --- --- --- --- | X | | | | | | Z | Z | Y | 7 --- --- --- --- --- --- --- --- --- | X | | | | | Z | Z | | Y | 6 --- --- --- --- --- --- --- --- --- | X | | | | Z | Z | | | Y | 5 --- --- --- --- --- --- --- --- --- | X | | | Z | Z | | | | Y | 4 --- --- --- --- --- --- --- --- --- | X | | Z | Z | | | | | Y | 3 --- --- --- --- --- --- --- --- --- | X | Z | Z | | | | | | Y | 2 --- --- --- --- --- --- --- --- --- | S |ZY | Y | Y | Y | Y | Y | Y | Y | 1 --- --- --- --- --- --- --- --- --- 1 2 3 4 5 6 7 8 9 The functions that you will write have the following specifications: ================================================== NumPaths (Row, Col, N) returns integer FUNCTION: Returns the number of paths possible to move in a 2-dimensional square grid from starting Row/Col to row N/column N. INPUT: Row (integer) - starting row PRECONDITIONS: 1 <= Row <= N 1 <= Col <= N OUTPUT: NumPaths (integer) POSTCONDITIONS: NumPaths = number of paths possible to move from starting row and column to terminal position at row N/ column N. (Note that even though the initial problem is to find the number of paths from (1,1) to (N,N), the solution involves writing a function that can count the paths from (a,b) to (N,N) where a and b are not necessarily equal.) ================================================== Both versions of NumPaths will be recursive functions. VERSION 1 The first version (call it NumPaths1) uses the "brute force" approach: IF Row = N OR Col = N THEN NumPaths <-- 1 ELSE NumPaths <-- NumPaths starting at next row/same column + NumPaths starting at same row/next column After you have coded the function, walk through the execution of NumPaths(1,1,4) by hand. Why is this algorithm inefficient? VERSION 2 The efficiency of this operation can be improved by keeping intermediate values of NumPaths in a two dimensional array of integer values. This allows the function to keep from having to calculate the same value more than once. Design and code a version of NumPaths (call it NumPaths2) that uses this approach. Be careful about where and how the array is declared and initialized! (You may want to make the recursive part a function nested inside NumPaths2. Also, if you are programming in Pascal, you need to decide on a fixed size for the array.) TESTING THE IMPLEMENTATION Write a test driver to test these functions. The driver must prompt the tester to input the value of N, then call each version of NumPaths to calculate the number of paths. The result of each call must be printed. The initial, non-recursive, calls are: PathCount := NumPaths1(1,1,N) ; and PathCount := NumPaths2(1,1,N) ; The test driver must continue prompting the tester to supply values of N until 0 is input. Run the program, using values of 1, 2, 3, ..., and so on, for N. Both versions should get the same results. At what point does the answer get too large for type integer? (You may want to change the result type of the funtions to Real.) COMPARING THE IMPLEMENTATIONS To compare the two versions of NumPaths, add a statement to each function to increment a variable called CallCount. CallCount will be used to see how many times the function is called. This variable must be declared as a global variable in the test driver. Before each initial (non-recursive) invocation of a NumPaths function, initialize CallCount to 0. After each such initial (non-recursive) call returns, print out CallCount. Label everything appropriately so it is easy for anyone running your program to understand the information. Record the results of CallCount for each function for values of N from 1 to 10. Answer the following questions: (1) How do the two versions of NumPaths compare in terms of execution-time efficiency? (2) How do the two versions of NumPaths compare in terms of space efficiency? TURN IN: 1. Level 1 version of the program, including all the test driver code, header comments for the main program and the two NumPaths functions, and stubs for the two versions of NumPaths. 2. Finished version of the program, including the code for the two versions of NumPaths. 3. The answers to questions (1) and (2) in the previous section. 4. Fill in the following chart with values calculated by your program: Number of Comparison of CallCount values N Paths NumPaths1 NumPaths2 1 2 3 4 5 6 7 8 9 10