Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101X: Programming Methodology Recitation 8 Recursion I

Similar presentations


Presentation on theme: "CS1101X: Programming Methodology Recitation 8 Recursion I"— Presentation transcript:

1 CS1101X: Programming Methodology Recitation 8 Recursion I

2 Task 1: Tracing Recursion #1 (1/3)
Trace the following recursive methods using various input data. Restrict your data to non-negative integers. From your results, describe in words what P() and R() do. If you interchange the two lines in method P() and call it P1(), what does P1() do? CS1101X Recitation #8

3 Task 1: Tracing Recursion #1 (2/3)
// pre-cond: n >= 0 public static void P(int n) { if (n < 10) System.out.print(n); else { P(n/10); // what if you swap System.out.print(n % 10); // these two lines } // pre-cond: n >= 0 public static void R(int n) { System.out.print(n % 10); if (n/10 != 0) R(n/10); } CS1101X Recitation #8

4 Task 1: Tracing Recursion #1 (3/3)
Trace the methods for n = 3792 For P : 3792 For P1: 2973 For R : 2973 CS1101X Recitation #8

5 Task 2: Tracing Recursion #2 (1/4)
Given the following method Q(). public static void Q(int n1, int n2) { if (n2 <= 0) System.out.println(); else { Q(n1 – 1, n2 – 1); System.out.print(n1 + " "); Q(n1 + 1, n2 – 1); } CS1101X Recitation #8

6 Task 2: Tracing Recursion #2 (2/4)
What output is produced by the call Q(14,3)? [Hint: First try Q(14,0), then Q(14,1), then Q(14,2).] If the System.out.print(n1 + " ") statement is moved before the first recursive call to Q(), what output will be produced by Q(14,3)? Write out a complete program to test this out. CS1101X Recitation #8

7 Task 2: Tracing Recursion #2 (3/4)
Write your outputs here. 12 13 14 15 16 CS1101X Recitation #8

8 Task 2: Tracing Recursion #2 (4/4)
Tracing Q(14,3) Q(16, 1) Q(17, 0) Q(15, 2) Q(15, 0) Q(13, 0) Q(11, 0) Q(14, 1) Q(12, 1) Q(13, 2) Q(14, 3) println “13” “12” “14” “15” “16” CS1101X Recitation #8

9 Task 3: Power method (1/2) The Math class provides the method
pow(double a, double b) that returns the value of the first parameter raised to the power of the second parameter. Write your own recursive method power(double x, int n) to compute xn, where n is a non-negative integer. For example, power(3.0, 4) = 3.0*3.0*3.0*3.0 = 81.0; and power(2.5, 3) = 2.5*2.5*2.5 = Test out your power() method and compare the results with the pow() method. CS1101X Recitation #8

10 Task 3: Power method (2/2) Write your method here.
// pre-cond: n >= 0 public static double power(double x, int n) { if (n == 0) return 1.0; else return x * power(x, n-1); } CS1101X Recitation #8

11 Task 4: Lining up children (1/2)
You need to line up n children in a queue. According to some unwritten rule, a girl may stand next to another girl or a boy, but two boys must not stand next to each other. Therefore, there are only 3 ways for a line of 2 children to be formed: boy-girl, girl-boy, and girl-girl. Write a recursive method lineup(int n) to compute the number of ways a line of n children may be formed. CS1101X Recitation #8

12 Task 4: Lining up children (2/2)
Write your method here. // pre-cond: n>= 1 public static int lineup(int n) { if (n == 1) return 2; else if (n == 2) return 3; else return lineup(n-1) + lineup(n-2); } CS1101X Recitation #8

13 Next week… We will do recursion on data structures (arrays).
CS1101X Recitation #8

14 End of Recitation #8 CS1101X Recitation #8


Download ppt "CS1101X: Programming Methodology Recitation 8 Recursion I"

Similar presentations


Ads by Google