In-class-exercise4-2019-0327.docx - Name Student ID CS206B 2019 SPRING 1 | P a g e Name Student ID Problem 1 Draw the recursion trace for computation of

# In-class-exercise4-2019-0327.docx - Name Student ID CS206B...

This preview shows page 1 - 5 out of 6 pages.

Name: Student ID: CS206B. 2019 SPRING 1 | P a g e
Name: Student ID: Problem 1. Draw the recursion trace for computation of power1(2,5) in Code Fragment 1: public static double power1( double x , int n ) { if ( n ==0) return 1; else return x * power1 ( x , n -1); } Code Fragment 1 Note: The example of a recursion trace can be found in the lecture slide #13 of Chapter 5. CS206B. 2019 SPRING 2 | P a g e
Name: Student ID: Problem 2. Draw the recursion trace for computation of power2(2, 18) in Code Fragment 2: public static double power2( double x , int n ) { if ( n ==0) return 1; else { double partial = power2 ( x , n /2); double result = partial * partial ; if ( n %2 == 1) result *= x ; return result ; } } Code Fragment 2 Note: The example of a recursion trace can be found in the lecture slide #13 of Chapter 5. CS206B. 2019 SPRING 3 | P a g e
Name: Student ID: Problem 3. Consider the following recursive implementation of the factorial function. public static long factorial( int n ) { if ( n == 0) { return 1; } else { return n * factorial ( n -1); } } Code Fragment 3 1) For factorial(3), how many times will the base case(s) be executed?

#### You've reached the end of your free preview.

Want to read all 6 pages?

• Summer '10
• Recursion, public static double power1