Recursionand getting to the root of a problem - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Recursionand getting to the root of a problem

Description:

Recursion that doesn't work motivating the need for a Base Case. Recall that recursive functions are ... Euclid actually phrased his GCD algorithm like this: ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 7
Provided by: need9
Category:

less

Transcript and Presenter's Notes

Title: Recursionand getting to the root of a problem


1
Recursionand getting to the root of a problem
  • Recursion that doesn't work motivating the need
    for a Base Case
  • Recall that recursive functions are functions
    whose definitions call themselves.
  • Before we look at how one devises a recursive
    function that accomplishes a given task, let's
    look at int f(int) a sorry little recursive
    function that just wont stop.
  • int f(int k)
  • int a kk
  • int b f(k 1)
  • return a b
  • Consider calling f(3). When does the recursion
    stop?

2
Base Cases Stopping the Recursion
  • Whether or not we are in the base case is
    determined by the parameters we're passed.
  • The base case here is k zero.
  • Provided the initial k is non-negative, we will
    eventually reach our base case, since the
    argument in each recursive call keeps getting
    closer to zero.
  • Consider calling sum(3)
  • int sum(int k)
  • int p 0,
  • ans 0
  • if (k 0) // Base case
  • return 0
  • // Recursive case (k-1)
  • p sum(k-1)
  • ans p k
  • return ans

3
Consider int sumSquare(int k) ie 12 22 32
... n2
  • The two keys to recursion are to find the base
    case(s), and to assume your function already
    works as you go about using recursive calls to
    define it.
  • What is the base case?
  • For what inputs can we automatically just spit
    out the answer without having to do any real
    work? In particular, without needing a recursive
    call?
  • Since we're assuming that k is positive, the
    easiest value for k is 1. If k is one we can just
    immediately return 1 without having to make any
    recursive calls.
  • So that gives us our base case of
  • if (k 1) // Base case
  • return 1

4
Consider int sumSquare(int k) ie 12 22 32
... n2
  • What is the recursive case? How would the answer
    to a "smaller" problem of the same kind help get
    the answer to the original problem.
  • In other words, if I assume that the function
    (which I already have the prototype for) just
    "works" for smaller inputs, how would using the
    function help me get to the answer?
  • Example How would the answer to sumSquare(k-1)
    help me figure out the answer to sumSquare(k)?
  • Just need to add k2 to the result of
    sumSquare(k-1), and we have the answer to
    sumSquare(k).
  • Gives our recursive case
  • // Recursive case
  • int p sumSquare(k - 1)
  • int ans p kk
  • return ans

5
Using recursive function sumSquare()
  • / Program uses recursion to return the sum of
    the squares 12 22 ... 102 /
  • include ltiostreamgt
  • using namespace std
  • int sumSquare(int k)
  • int main()
  • int a, n 10
  • a sumSquare(n)
  • cout ltlt a
  • return 0
  • int sumSquare(int k)
  • // Base case
  • if (k 1)
  • return 1 // 12 is 1
  • // Recursive case
  • //k-1 winds down towards base case
  • int p sumSquare(k - 1)
  • // builds answer
  • int ans p kk
  • return ans

6
ICE A recursive version of GCD
  • Euclid actually phrased his GCD algorithm like
    this
  • If A and B are two integers with A gt B gt 0 then
    the GCD of A and B is given as
  • if B 0 then the GCD is A
  • if B gt 0 then the GCD is the same as the GCD of B
    and AB.
  • Using this phrasing of the algorithm, implement
    GCD as a recursive function.
  • Think, what's the base case? What's the recursive
    case? Does the recursive case work towards the
    base case? (it better!)
  • include ltiostreamgt
  • using namespace std
  • int GCD(int,int)
  • int main()
  • int x, y, z
  • cout ltlt "Enter two int's "
  • cin gtgt x gtgt y
  • // Compute and print GCD
  • z GCD(x,y)
  • cout ltlt "GCD is " ltlt z
  • ltlt endl
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com