CS 210 – Fundamentals of Programming I
Spring 2007 – Homework 5
20 points
Out: April 16/17
Due: April 23/24


1. (5 points) Consider the following recursive function.


int Choose (int n, int k)
{
   if (k == 1)
      return n;
   else if (n == k)
      return 1;
   else
      return Choose (n-1, k-1) + Choose (n-1, k);
}


a. What is/are the base case(s) of this function?

b. What is/are the recursive step(s) of this function?

c. What is the result of the function call Choose(5,2)?


2. (5 points) Write the analysis and design for a recursive function NumDigits that returns the number of digits in a non-negative integer. E.g. NumDigits(2485) returns 4 while NumDigits(32) returns 2.


3. (5 points) Write the C++ function definition for the recursive NumDigits function designed in the previous exercise.


4. (5 points) Write a C++ function definition for an iterative (i.e., non-recursive) version of NumDigits.

04/13/07 1