12recursive_func

12recursive_func - Introduction to Computers and...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computers and Programming Lecture 12 Prof. I. K. Lundqvist Reading: B pp. 181-189, FK pp. 591-621, handout Oct 1 2003 PRS 1 -- Scope 1. Nothing is displayed on the screen 2. 0 (zero) is displayed on the screen 3. 7 is displayed on the screen 4. Don’t know
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Recursion call themselves base case) is reached that can be solved immediately General algorithm if stopping condition then solve simple problem else use recursion to solve smaller problem(s) combine solutions from smaller problem(s) • Writing procedures and functions which •Invo lves – Solving large problems – By breaking them into smaller problems –Of ident ica l form • Eventually, a “trivial” problem (the •I tera t ion –Cogn it ive s imp le •Recurs – Is not as intuitive – Demanding on machine time and memory – Sometimes simpler than iteration
Background image of page 2
Guess a number Problem : think of a number in the range 1 to N Reworded : guess again Recursion comes into the “ guess again stage Factorial n , computes n! n! = 1 2 ... ( n -1) n 5! = 1 2 3 4 5 = 120 Receive: n , an integer Precondition: n >= 0 (0! = 1 and 1! = 1) Return: n! Given a set of N possible numbers to choose from Guess a number from the set –I f w rong , Continue until the number is guessed successfully A set of N-1 numbers remains from which to guess This is a smaller version of the same problem • Write a function that, given •Examp le: • Specification:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Preliminary Analysis function return Positive is Result : Positive; begin Result := 1 ; for Count in 2 .. N loop
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

12recursive_func - Introduction to Computers and...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online