Recursion PowerPoint PPT Presentation

presentation player overlay
1 / 4
About This Presentation
Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • State the problem recursively
  • A well-defined base case
  • A recursive call that guarantees a smaller
    problem and eventually leads to the base case.
  • If no base case specified, or you never reach the
    base case, youll have infinite recursion.
  • For the recursive call, consider whether what
    actions (if any) need to be performed, and
    whether the action happens before or after the
    recursive call.

2
Recursion - xn
  • Write a method Power(x,n) which returns xn
  • Base case xn x, if n 1
  • Recursive cases xn x xn-1, if ngt1

3
Recursion output digits
  • Take an integer, write the digits one at a time
    on separate lines.
  • WriteDigits(1234), output
  • 1
  • 2
  • 3
  • 4
  • Base case
  • Output n, for n lt 10
  • Recursive case
  • Output digits of (n/10), then output n10, for
    ngt10.
  • What if we want to output the digits in a reverse
    order?

4
About the test
  • Review session on Wed.
  • Polymorphism, exception handling, recursion.
  • C windows controls.
  • Zhihongs office hours
  • Tue Thur 400P 530P
  • Thur 1030P 1220P (NO discussion!)
Write a Comment
User Comments (0)
About PowerShow.com