ch5 - 2001-2009 M. D. Evans All Rights Reserved 1 Ch. 5...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2001-2009 M. D. Evans All Rights Reserved 1 Ch. 5 Recursion: 5.1 Introduction: A problem is said to have a recursive solution if that solution can be expressed in a way which seems to include a reproduction of a smaller representation of the original problem. Examples: - set of Russian dolls - x n = x x n-1- n! = n (n-1)! - print word in reverse = (print Last Letter)(print rest of word in reverse) 2001-2009 M. D. Evans All Rights Reserved 2 Designing a recursive solution requires: - we find a natural recursive expression of the solution. - 2 cases be considered: 1. base case soln expressed non-recursively 2. general case soln expressed recursively 2001-2009 M. D. Evans All Rights Reserved 3 5.2 Activation Records a manifestation of a program in execution. each program block ie a main program, a function or subprogram, manifested by its own AR. each invocation of a block builds a new AR record for that block. at end of block execution, the AR is destroyed (with the consequences of: ....). permit management of - flow of execution control from one subprog to another. - extent management of compile-time variables. 2001-2009 M. D. Evans All Rights Reserved 4 a data structure containing - attributes: - Space, values for local vars. - Code to be executed. - Return information. - operations to: - create an AR - move it to the cpu - update its contents - etc AR for a subprogram is stacked onto a system stack when it invokes another subprogram. If global identifier referenced search of ARs from stack-top until 1 st occurrence of that identifier. 2001-2009 M. D. Evans All Rights Reserved 5 Example: main System Stack ... Call a() ... Call b() PROC a() ... Call b() ... PROC b() CPU 2001-2009 M. D. Evans All Rights Reserved 6 5.3 Detailed Example of Recursion. The Problem: Consider inputting a word and writing it in reverse. The Recursive Solution: PROGRAM Reverse() PROCEDURE echoReversed() VAR inchar:char; BEGIN (*echoReversed*) IF eoln THEN writeln ELSE read (inchar) echoReversed() {recursive call} write (inchar) ENDIF; END; (*echoReversed*) Begin (*main*) writeln (Enter word:) echoReversed() {non-recursive call} writeln END. (*main*)...
View Full Document

Page1 / 24

ch5 - 2001-2009 M. D. Evans All Rights Reserved 1 Ch. 5...

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

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