cop3502 introduction to cis i n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 12 PowerPoint Presentation
Download Presentation
Lecture 12

Loading in 2 Seconds...

play fullscreen
1 / 19

Lecture 12 - PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on

COP3502: Introduction to CIS I. Lecture 12. r ecursion “defining a program in terms of itself”. “find your way home”. f ind your way home: if (you are at home) { stop moving } else { take one step towards home “find your way home” } . “find your way home”.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 12' - minowa


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide2

recursion

“defining a program in terms of itself”

find your way home
“find your way home”

find your way home:

if (you are at home) {

stop moving

}

else {

take one step towards home

“find your way home”

}

find your way home1
“find your way home”

find your way home (stepsAway) :

if (stepsAway == 0) {

stop moving

}

else {

take one step towards home

“find your way home”(stepsAway – 1)

}

slide5

recursion requirements

base case – recursion ends

recursive call – function call on smaller problem

EVERY RECURSIVE CALL SHOULD BRING YOU CLOSER TO THE BASE CASE!

slide6

factorial

n! = n x (n-1) x (n-2) …. x 2 x 1

Ex. 5! = 5 x 4 x 3 x 2 x 1 = 120

slide7

factorial

n! = n x (n-1)!

5! = 5 x 4!

= 5 x (4 x 3!)

= 5 x (4 x (3 x 2!))

= 5 x (4 x (3 x (2 x 1!)))

slide9

FACTORIAL(5) = 5 * FACTIORIAL(4)

FACTORIAL(4) = 4 * FACTIORIAL(3)

slide10

FACTORIAL(5) = 5 * FACTIORIAL(4)

FACTORIAL(4) = 4 * FACTIORIAL(3)

FACTORIAL(3) = 3 * FACTIORIAL(2)

slide11

FACTORIAL(5) = 5 * FACTIORIAL(4)

FACTORIAL(4) = 4 * FACTIORIAL(3)

FACTORIAL(3) = 3 * FACTIORIAL(2)

FACTORIAL(2) = 2 * FACTIORIAL(1)

slide12

FACTORIAL(5) = 5 * FACTIORIAL(4)

FACTORIAL(4) = 4 * FACTIORIAL(3)

FACTORIAL(3) = 3 * FACTIORIAL(2)

FACTORIAL(2) = 2 * FACTIORIAL(1)

FACTORIAL(1) = 1 * FACTIORIAL(0)

slide13

FACTORIAL(5) = 5 * FACTIORIAL(4)

FACTORIAL(4) = 4 * FACTIORIAL(3)

FACTORIAL(3) = 3 * FACTIORIAL(2)

FACTORIAL(2) = 2 * FACTIORIAL(1)

FACTORIAL(1) = 1 * FACTIORIAL(0)

FACTORIAL(0) = 1

BASE CASE!

slide14

Fibonacci

Fib(0) = 0

Fib(1) = 1

Fib(2) = 1

Fib(3) = 2

Fib(n) = Fib(n-1) + Fib(n-2)