Quiz 4

Please write your name and lab section letter at the top of the page.

Consider the following recursive program

  public int mpy(int a, int b) { // a and b must be nonnegative
     /* reserved for future use */   /* line 1 */
     if (b == 0)                     /* line 2 */
          return 0;                  /* line 3 */
     else return a + mpy(a, b-1); 
  }
  1. (10 pts) What is the base case?
  2. (25 pts) Show by substitution the evaluation of mpy(4,3).
  3. (25 pts) Show by data flow model the evaluation of mpy(5,1)
  4. (20 pts) If the predicate in line 2 were changed to
        if (b == 1)
    
    how would you change line 3 so that the method is correct?
  5. (10 pts) What happens to the cost of running this method when b is much, much (much) greater than a?
  6. (10 pts) What would you put at line 1 so that the running time of this method is insensitive to b being much, much (much) greater than a?