Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Similar presentations


Presentation on theme: "CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING."— Presentation transcript:

1 CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

2 The problem with rabbits… A man puts a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive?

3 Fibonacci’s rabbits.. Fibonacci numbers were invented to model the growth of a rabbit colony fib 1 = 1 fib 2 = 1 fib n = fib n-1 + fib n-2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

4 Recursive Thinking CS340 4

5 Recursive Thinking Recursion reduces a problem into one or more simpler versions of itself CS340 5

6 Recursive Thinking (cont.) CS340 6

7 Recursive Thinking (cont.) Recursion of Process Nested Dreams A child couldn't sleep, so her mother told a story about a little frog, who couldn't sleep, so the frog's mother told a story about a little bear, who couldn't sleep, so bear's mother told a story about a little weasel...who fell asleep....and the little bear fell asleep;...and the little frog fell asleep;...and the child fell asleep. CS340 7

8 Steps to Design a Recursive Algorithm  Base case:  for a small value of n, it can be solved directly  Recursive case(s)  Smaller versions of the same problem  Algorithmic steps:  Identify the base case and provide a solution to it  Reduce the problem to smaller versions of itself  Move towards the base case using smaller versions CS340 8

9 Finding… a needle in a haystack This is a classical computational thinking problem Write a function: find_needle Inputs of your function: the number that you are looking for and an array of numbers Outputs of your function: the index of the array where the number was found, OR a message if the number is not in the array.

10 Recursive Thinking (cont.) Consider searching for a target value in an array With elements sorted in increasing order Compare the target to the middle element If the middle element does not match the target search either the elements before the middle element or the elements after the middle element Instead of searching n elements, we search n/2 elements CS340 10

11 Recursive Thinking (cont.) Recursive Algorithm to Search an Array if the array is empty return -1 as the search result else if the middle element matches the target return the subscript of the middle element as the result else if the target is less than the middle element recursively search the array elements before the middle element and return the result else recursively search the array elements after the middle element and return the result CS340 11

12 Recursive Algorithm for Finding the Length of a String if the string is empty (has no characters) the length is 0 else the length is 1 plus the length of the string that excludes the first character CS340 12

13 Recursive Definitions of Mathematical Formulas CS340 13

14 Recursive Definitions of Mathematical Formulas Mathematicians often use recursive definitions of formulas Examples include: factorials powers greatest common divisors (gcd) CS340 14

15 Factorial of n: n! The factorial of n, or n! is defined as follows: 0! = 1 n! = n x (n -1)! (n > 0) The base case: n equal to 0 The second formula is a recursive definition CS340 15

16 Factorial of n: n! (cont.)  The recursive definition can be expressed by the following algorithm: if n equals 0 n! is 1 else n! = n x (n – 1)!  The last step can be implemented as: return n * factorial(n – 1); CS340 16

17 Infinite Recursion and Stack Overflow  Call factorial with a negative argument, what will happen? CS340 17 StackOverflowException

18 Resources Lecture slides CS112, Ellen Hildreth, http://cs.wellesley.edu/~cs112/ http://cs.wellesley.edu/~cs112/

19 QUESTIONS??


Download ppt "CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING."

Similar presentations


Ads by Google