CS201 Spring 2001
Prof. Hartman
Homework #5
Due Friday, February 23rd
by 5:00 pm
This programming assignment is a bit shorter, since you will have a test on Friday, February 23rd.
Recommended Self-Review problems in Chapter 3, pp223-226 3.8, 3.9, 3.10
Homework to be turned in (remember that all programs must
follow the guidelines from handout #3.
Notice that two of the following problems require written comments as well as code. (Its fine with me if you make these comments as a comment after your code or whatever, but I want to see these explanations.)
Programming:
1)
Write a recursive function power(base, exponent) that calculates baseexponent.
For example, power(2,5) =
2*2*2*2*2 = 32. Assume exponent
is an integer greater than or equal to one, and make base of type double. (Hint: the recursion step will
use the relationship
baseexponent = base(exponent 1)* base. The base case
would be when exponent is
one.)
2)
Do
problem 3.44 in the text from page 235. Explain how your output could help
somebody understand recursion. (Hint: remember that a static variable in a
function exists (and is initialized) only once. You can use a static variable
in your recursive function. If you increment this variable at the beginning of
the function and decrement it at the end, it will always be equal to the
recursion depth.)
3)
Write
a program that, in main(),declares two integer variables and initializes them to 0.
Write a function that takes two integer parameters, one by value and one by
reference. In your function, set each of the parameters to 2. Call your
function from main() with the
two variables as the two parameters. Output the values of your variables before
and after the function call. Explain in
your own words the results.