Docsity

CSE 232 Lab Exercise 14: Recursive Functions, Lab Reports of Computer Science

Toggle sidebar
bg1
CSE 232 Spring, 2009 Name: __________________________________
Lab Exercise #14 -- Recursive Functions
Copy all of the files named "~cse232/Labs/lab14.*" into your account, then
perform the following experiments.
1. Examine the "driver" module, which uses command-line arguments. Assuming
that the executable program is named "lab14.exe", give the two different valid
commands to execute the program.
_______________________________________________________________________________
_______________________________________________________________________________
2. Examine the "lucas" module and list the declarations for the three functions
which are part of that module.
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
3. Only one of those three functions is accessible outside of the "lucas"
module. Which one? What mechanism did the programmer use to prevent the
other two functions from being accessible outside of the module? Explain.
4. Add the appropriate statements to "lucas_non" to complete that function.
The following sub-algorithm may be useful:
Back2 <-- 1
Back1 <-- 3
loop from 3 to N
Result <-- Back1 + Back2
Back2 <-- Back1
Back1 <-- Result
endloop
Test your completed program to be sure that both implementations for computing
the Lucas numbers are correct.
5. Compare the two implementations for computing the Lucas numbers. Which
implementation most closely matches the definition of the Lucas numbers given
in the comments for function "lucas"? Which implementation uses the least
system resources (CPU cycles and primary storage)? Explain.
pf2

Partial preview of the text

Download CSE 232 Lab Exercise 14: Recursive Functions and more Lab Reports Computer Science in PDF only on Docsity!

CSE 232 Spring, 2009 Name: __________________________________

Lab Exercise #14 -- Recursive Functions

Copy all of the files named "~cse232/Labs/lab14.*" into your account, then perform the following experiments.

  1. Examine the "driver" module, which uses command-line arguments. Assuming that the executable program is named "lab14.exe", give the two different valid commands to execute the program.

_______________________________________________________________________________

_______________________________________________________________________________

  1. Examine the "lucas" module and list the declarations for the three functions which are part of that module.

_______________________________________________________________________________

_______________________________________________________________________________

_______________________________________________________________________________

  1. Only one of those three functions is accessible outside of the "lucas" module. Which one? What mechanism did the programmer use to prevent the other two functions from being accessible outside of the module? Explain.
  2. Add the appropriate statements to "lucas_non" to complete that function. The following sub-algorithm may be useful:

Back2 <-- 1 Back1 <-- 3 loop from 3 to N Result <-- Back1 + Back Back2 <-- Back Back1 <-- Result endloop

Test your completed program to be sure that both implementations for computing the Lucas numbers are correct.

  1. Compare the two implementations for computing the Lucas numbers. Which implementation most closely matches the definition of the Lucas numbers given in the comments for function "lucas"? Which implementation uses the least system resources (CPU cycles and primary storage)? Explain.
  1. Add the following statements to "lucas_rec" (in the appropriate places):

cout << "+++ Entering N: " << N << " Count: " << Count << endl;

cout << "--- Exiting N: " << N << " *** Result: " << Result << endl;

Then, use the program to recursively compute f(6). In the space below, draw a "call graph" (similar to the one on page 529 of the Nyhoff textbook) for f(6). Be sure to label each branch in the call graph to indicate temporal order ("A" for the first call, "B" for the second call, etc).

  1. Write a complete program to compute the value of Ackermann’s function, which is defined below:

f( m, n ) = n+1 if m = 0 f( m, n ) = f( m-1, 1 ) if n = 0 f( m, n ) = f( m-1, f( m, n-1 ) ) otherwise

The program should count and display the number of function calls that are required to compute a specified function value.

Warning: even for small argument values, Ackermann’s function requires many recursive function calls.

  1. Demonstrate your completed programs to your TA.