CS1101X: Programming Methodology Recitation 8 Recursion I - PowerPoint PPT Presentation

About This Presentation
Title:

CS1101X: Programming Methodology Recitation 8 Recursion I

Description:

Restrict your data to non-negative integers. ... to some unwritten rule, a girl may stand next to another girl or a boy, but two ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 15
Provided by: aaro3
Category:

less

Transcript and Presenter's Notes

Title: CS1101X: Programming Methodology Recitation 8 Recursion I


1
CS1101X Programming MethodologyRecitation 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?
3
Task 1 Tracing Recursion 1 (2/3)
// pre-cond n gt 0 public static void P(int n)
if (n lt 10) System.out.print(n)
else P(n/10) // what
if you swap System.out.print(n 10) //
these two lines
// pre-cond n gt 0 public static void R(int n)
System.out.print(n 10) if (n/10 ! 0)
R(n/10)
4
Task 1 Tracing Recursion 1 (3/3)
Trace the methods for n 3792
For P 3792 For P1 2973 For R 2973
5
Task 2 Tracing Recursion 2 (1/4)
Given the following method Q().
public static void Q(int n1, int n2) if
(n2 lt 0) System.out.println() else
Q(n1 1, n2 1) System.out.print(n1
" ") Q(n1 1, n2 1)
6
Task 2 Tracing Recursion 2 (2/4)
  1. What output is produced by the call Q(14,3)?
    Hint First try Q(14,0), then Q(14,1), then
    Q(14,2).
  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)?
  3. Write out a complete program to test this out.

7
Task 2 Tracing Recursion 2 (3/4)
Write your outputs here.
12 13 14 14 14 15 16
8
Task 2 Tracing Recursion 2 (4/4)
Tracing Q(14,3)
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.03.03.03.0 81.0 and
power(2.5, 3) 2.52.52.5 15.625. Test out
your power() method and compare the results with
the pow() method.
10
Task 3 Power method (2/2)
Write your method here.
// pre-cond n gt 0 public static double
power(double x, int n) if (n 0)
return 1.0 else return x power(x,
n-1)
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.
12
Task 4 Lining up children (2/2)
Write your method here.
// pre-cond ngt 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)
13
Next week
We will do recursion on data structures (arrays).
14
End of Recitation 8
Write a Comment
User Comments (0)
About PowerShow.com