Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted.

Similar presentations


Presentation on theme: "Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted."— Presentation transcript:

1 Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted Due Friday, March 11 Due Friday, March 11 Sample recursive program on the course Resources page Sample recursive program on the course Resources page

2 Today's topics Recursion Recursion Low(er)-level programming Low(er)-level programming Program #5 Program #5 Reverse Polish Notation (RPN) Reverse Polish Notation (RPN) Expression evaluation Expression evaluation

3 Recursion Many processes are defined by using previous results of the same process Many processes are defined by using previous results of the same process Example: summation (a, b)when a  b Example: summation (a, b)when a  b Iterative definition: Iterative definition: summation(a, b) = a + (a+1) + (a+2) + … + b summation(a, b) = a + (a+1) + (a+2) + … + b Recursive definition: Recursive definition:

4 Recursion Note that the definition has two parts Note that the definition has two parts base caserecursive part

5 Recursion in computer programs Recursion occurs in programs when: Recursion occurs in programs when: A procedure calls itself A procedure calls itself Procedure A calls procedure B, which in turn calls procedure A Procedure A calls procedure B, which in turn calls procedure A Calls are repeated in a cycle of procedure calls Calls are repeated in a cycle of procedure calls Recursion in programs mirrors recursive definitions Recursion in programs mirrors recursive definitions

6 Example (pseudo-code) function summation (a,b) returns sum of integers from a to b. preconditions: a <= b function summation (int a, int b): if a == b return a else return a + summation(a+1,b)

7 demo8.asm Recursive version of summation problem Recursive version of summation problem Issues: Issues: Using stack frames* for recursion is essential. Using stack frames* for recursion is essential. Why? Why? What causes stack overflow? What causes stack overflow? Why pass all 3 parameters (since 2 of them never change)? Why pass all 3 parameters (since 2 of them never change)? *stack frame, activation frame, activation record

8 Recursion Warnings A good mathematical recursive definition does not necessarily imply a recursive procedure. A good mathematical recursive definition does not necessarily imply a recursive procedure. Example: Fibonacci sequence Example: Fibonacci sequence Be sure that Be sure that the base case is defined the base case is defined the base case is reachable. the base case is reachable. the recursive calls approach the base case. the recursive calls approach the base case. Infinite (or too much) recursion results in "stack overflow" Infinite (or too much) recursion results in "stack overflow"

9 Questions on recursion? Questions on recursion?

10 Lower-level Programming All keyboard input is character All keyboard input is character Digits are character codes 48 – 57 Digits are character codes 48 – 57 '0' = 48, '1' = 49, … '9' = 57 '0' = 48, '1' = 49, … '9' = 57 What does ReadInt do? (Irvine's library) What does ReadInt do? (Irvine's library) Gets a string of digits (characters) Gets a string of digits (characters) Converts digits to numeric values Converts digits to numeric values How does ReadInt do it? How does ReadInt do it?

11 ReadInt algorithm (pseudo-code) get str x = 0 for k = 0 to (len(str) – 1) if 48 <= str[k] <= 57 x = 10 * x + (str[k] - 48) elsebreak

12 Program #5: Combinatorics Drill/Practice Generate two random numbers Generate two random numbers n in [3.. 12] n in [3.. 12] r in [1.. n] r in [1.. n] Calculate n C r Calculate n C r Number of ways to choose r elements from a set of n elements Number of ways to choose r elements from a set of n elements Get and evaluate student's answer Get and evaluate student's answer Show correct answer Show correct answer Repeat until user chooses to quit Repeat until user chooses to quit

13 Program #5: Combinatorics Drill/Practice Requirements (not a complete list): Requirements (not a complete list): Must get user input the “hard way” Must get user input the “hard way” as a string … so don’t use Irvine’s ReadInt as a string … so don’t use Irvine’s ReadInt OK to use Irvine’s ReadString OK to use Irvine’s ReadString Factorial calculation must be recursive Factorial calculation must be recursive Questions on Program #5 ? Questions on Program #5 ?

14 Reverse Polish Notation RPN RPN Postfix form of expression Postfix form of expression Example Example Infix:a + (b – c) * (d + e) Infix:a + (b – c) * (d + e) Postfix (RPN):abc-de+*+ Postfix (RPN):abc-de+*+

15 Conversion infix  postfix (RPN) Binary tree method Fully parenthesize infix Fully parenthesize infix Don’t parenthesize postfix Don’t parenthesize postfix Operands are always in the original order Operands are always in the original order Operators may appear in different order. Operators may appear in different order.

16 Conversion infix  postfix (RPN) Binary tree method 1. Fully parenthesize the infix expression Follow rules of operator precedence Follow rules of operator precedence 2. Parse the expression left to right, constructing a binary tree (go left (go left Operandinsert Operandinsert Operatorgo up, insert, go right Operatorgo up, insert, go right )go up )go up Post-order traversal gives RPN Post-order traversal gives RPN

17 Examples (infix → postfix) (a + b) * (c + d) + e (a + b) * (c + d) + e ((a + b) * (c + d)) + e ((a + b) * (c + d)) + e ab+cd+*e+ ab+cd+*e+ a * b + c * d * e a * b + c * d * e (a * b) + ((c * d) * e) (a * b) + ((c * d) * e) ab*cd*e*+ ab*cd*e*+ (a - b) * (((c - d * e) / f) / g) * h (a - b) * (((c - d * e) / f) / g) * h ((a - b) * (((c – (d * e)) / f) / g)) * h ((a - b) * (((c – (d * e)) / f) / g)) * h ab-cde*-f/g/*h* ab-cde*-f/g/*h*

18 Conversion postfix → infix Binary tree method Binary tree method Diagram expression as a binary tree Diagram expression as a binary tree last operator is root last operator is root Do in-order traversal, parenthesizing each subtree Do in-order traversal, parenthesizing each subtree

19 Examples (postfix → infix) ab+c+d* ab+c+d* ((a + b) + c) * d ((a + b) + c) * d abcde+**/ abcde+**/ a/(b*(c*(d+e))) a/(b*(c*(d+e))) abcde*f/+g-h/*+ abcde*f/+g-h/*+ a+(b*(((c+((d*e)/f))-g)/h)) a+(b*(((c+((d*e)/f))-g)/h))

20 Evaluation of RPN expressions Parse expression left to right, creating a stack of operands Parse expression left to right, creating a stack of operands operandpush onto stack operandpush onto stack operatorpop 2 operands, perform operatorpop 2 operands, perform operation, push result onto stack Single value remaining on stack is value of expression Single value remaining on stack is value of expression

21 Examples (Evaluation of RPN expressions) a = 5, b = 7, c = 4, d = 2, e = 3, f = 1, g = 6 a = 5, b = 7, c = 4, d = 2, e = 3, f = 1, g = 6 ab+cd*- ab+cd*- 4 abcdefg++**++ abcdefg++**++ 92 92 abc+de*f/+g-* abc+de*f/+g-* 55 55

22 Using RPN in programs Expression evaluation Expression evaluation 0-address machine 0-address machine e.g., Intel IA-32 FPU e.g., Intel IA-32 FPU Example: Evaluate a – b * c Example: Evaluate a – b * c 1. Convert to RPN abc*- 2. Program pusha pushb pushc mulsub

23 Stack instructions (one-address) InstructionMeaning PUSH M Push the contents of memory address M onto the stack. POP M Pop the top of the stack into memory address M Arithmetic instructions zero-address one-address two-address three-address

24 Zero-address arithmetic instructions InstructionMeaning ADD Pop and add the top two values on the stack; push the result back onto the stack. SUB Subtract the top from the next-to-top; pop the top two items and push the result. MUL Pop and multiply the top two values on the stack; push the result back onto the stack. DIV Divide the next-to-top by the top; pop the top two items and push the result.

25 a = b – (c + d) 0-address: use RPN 0-address: use RPN a = b c d + - pushb pushc pushd addsub popa

26 Questions? Do Homework #2 Get moving on Program #5


Download ppt "Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted."

Similar presentations


Ads by Google