19
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

CS 170 – Intro to Scientific and engineering Programming

  • Upload
    bevis

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

CS 170 – Intro to Scientific and engineering Programming . The problem with rabbits… . A man puts a pair of rabbits in a place surrounded on all sides by a wall. - PowerPoint PPT Presentation

Citation preview

Page 1: CS 170 – Intro to Scientific  and engineering Programming

CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

Page 2: CS 170 – Intro to Scientific  and engineering Programming

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?

Page 3: CS 170 – Intro to Scientific  and engineering Programming

Fibonacci’s rabbits..• Fibonacci numbers were invented to model the growth of

a rabbit colonyfib1 = 1fib2 = 1fibn = fibn-1 + fibn-2

• 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

Page 4: CS 170 – Intro to Scientific  and engineering Programming

4

Recursive Thinking

Click icon to add picture

CS340

Page 5: CS 170 – Intro to Scientific  and engineering Programming

CS340 5

Recursive Thinking• Recursion reduces a problem into one or more simpler

versions of itself

Page 6: CS 170 – Intro to Scientific  and engineering Programming

CS340 6

Recursive Thinking (cont.)

Page 7: CS 170 – Intro to Scientific  and engineering Programming

CS340 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.

Page 8: CS 170 – Intro to Scientific  and engineering Programming

CS340 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

Page 9: CS 170 – Intro to Scientific  and engineering Programming

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.

Page 10: CS 170 – Intro to Scientific  and engineering Programming

CS340 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

Page 11: CS 170 – Intro to Scientific  and engineering Programming

CS340 11

Recursive Thinking (cont.)Recursive Algorithm to Search an Arrayif the array is empty

return -1 as the search resultelse 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

Page 12: CS 170 – Intro to Scientific  and engineering Programming

CS340 12

Recursive Algorithm for Finding the Length of a Stringif the string is empty (has no characters)

the length is 0else

the length is 1 plus the length of the string that excludes the first character

Page 13: CS 170 – Intro to Scientific  and engineering Programming

13

Recursive Definitions of Mathematical Formulas

CS340

Page 14: CS 170 – Intro to Scientific  and engineering Programming

CS340 14

Recursive Definitions of Mathematical Formulas

• Mathematicians often use recursive definitions of formulas• Examples include:

• factorials• powers• greatest common divisors (gcd)

Page 15: CS 170 – Intro to Scientific  and engineering Programming

CS340 15

Factorial of n: n!• The factorial of n, or n! is defined as follows:

0! = 1n! = n x (n -1)! (n > 0)

• The base case: n equal to 0• The second formula is a recursive definition

Page 16: CS 170 – Intro to Scientific  and engineering Programming

CS340 16

Factorial of n: n! (cont.) The recursive definition can be expressed by the

following algorithm:if n equals 0

n! is 1else

n! = n x (n – 1)! The last step can be implemented as:

return n * factorial(n – 1);

Page 17: CS 170 – Intro to Scientific  and engineering Programming

CS340 17

Infinite Recursion and Stack OverflowCall factorial with a negative argument, what will

happen?

StackOverflowException

Page 18: CS 170 – Intro to Scientific  and engineering Programming

Resources• Lecture slides CS112, Ellen Hildreth,

http://cs.wellesley.edu/~cs112/

Page 19: CS 170 – Intro to Scientific  and engineering Programming

QUESTIONS??