Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed. 4-12-2018.

Similar presentations


Presentation on theme: "Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed. 4-12-2018."— Presentation transcript:

1 Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed.

2 begin a:= 10; b:=6; writeln(a:4, b:4); c(b,a); b:=d(a,b); end.
Program DryRuin; var a,b:integer; procedure c(var a:integer;b: integer); begin a:= 10 - a; b:= 3*a; writeln(a:4,b:4); end; function d(e,f:integer):integer; g:integer; g:= 5; e:= g+3; f:= 3 - 6; writeln(e:4, f:4, g:4); d:= e+f+g; begin a:= 10; b:=6; writeln(a:4, b:4); c(b,a); b:=d(a,b); end.

3 Learning Objectives Be able to dry run a program that has functions.
Understand how recursion works and be able to write a program that uses recursion. See #2

4 program sample; {****************************************} procedure tale; begin Write('Twas a dark and stormy night, '); writeln( 'and the captain said to his crew, '); write(‘ "Gather round, and I''ll tell ye a tale.“ '); writeln('So the crew gathered round, '); writeln(' and the captain said: '); readln; tale; end; {***************************************} end.

5 Dry run program sumthing; function sum (no : integer): integer; begin
if (no = 1) then sum := 1 else sum:= no - sum (no - 1); end; var total : integer; total := sum(3); writeln(total); end. Dry run

6 Dry run the following Function mystery(n:integer):integer; Begin
if n=0 then mystery:=1 else mystery:=3*mystery(n-1); End; What is the result of mystery(4)?

7 Reading a program with Recursion
program Confusion; function a(b:integer):integer; begin if b<= 1 then a:=b else a:= 2*b + a(b-1); end; writeln(a(4)); readln; end.

8 Program yppah; Procedure smile(n:integer); var k:integer; begin if (n >0)then for k:= 1 to n do write('smile! '); smile(n-1); end; Begin smile(4); Readln; End.

9 Recursion quotes. "In order to understand recursion, one must first understand recursion." Definition Recursion If you still don't get it, See: "Recursion".

10 Recursion Recursion: When a subroutine calls itself.
When designing a recursive function it is imperative to include the following four elements: a well-defined BASE CASE, a RECURSIVE CALL within the body of the function, a guaranteed SMALLER PROBLEM as a result of the recursive call, and a guarantee that the BASE CASE IS REACHABLE

11 When To Use Recursion • When a problem can be divided into steps.
• The result of one step can be used in a previous step. • There is scenario when you can stop sub-dividing the problem into steps and return to previous steps. • All of the results together solve the problem.

12 When To Consider Alternatives To Recursion
• When a loop will solve the problem just as well • Types of recursion: • Tail recursion — A recursive call is the last statement in the recursive module. — This form of recursion can easily be replaced with a loop. • Non-tail recursion — A statement which is not a recursive call to the module comprises the last statement in the recursive module. —This form of recursion is very difficult to replace with a loop.

13 Drawbacks Of Recursion
Function/procedure calls can be costly • Uses up memory • Uses up time

14 Benefits Of Using Recursion
• Simpler solution that’s more elegant (for some problems) • Easier to visualize solutions (for some people and certain classes of problems – typically require either: non-tail recursion to be implemented or some form of “backtracking”)

15 Common Pitfalls When Using Recursion
•These three pitfalls can result in a segmentation fault (using up too much memory) occurring • No base case • No progress towards the base case • Using up too many resources (e.g., variable declarations) for each function call

16 Reading a program with Recursion
program Confusion; function a(b:integer):integer; begin if b<= 1 then a:=b else a:= b + a(b-1); end; writeln(a(4)); readln; end.

17 Applying Recursion Powers ab Base Case Smaller Problem Recursive Call
an = a * a(n-1) Recursive Call power = base * power(exponent - 1)

18 Example Program Recursion;
function pow(base,exponent:integer):integer; begin if exponent>=1 then pow:= base*pow(base,exponent-1) else pow:=1; end; a:=3; b:=4; writeln(pow(a,b)); end. Recursive call Smaller Problem Base Case

19 Dry run program sumthing; function sum (no : integer): integer; begin
if (no = 1) then sum := 1 else sum:= no - sum (no - 1); end; var total : integer; total := sum(5); writeln(total); end. Dry run

20 Program Factorial using recursion Input: 10 names into an array
Input: A positive integer Output: It’s factorial: Example: Input 6 Output: 6 ! = 6*5*4*3*2*1 = 720 Input: 10 names into an array Output: The names in reverse order recursively, in the subroutine Finding the Greatest Common Factor using the Euclidean Algorithm If b goes into a, then the GCF(a,b) is b If b does not go into a, then GCF(a,b) = GCF(b, a MOD b) Example: GCF(28,8) since 28 MOD 8 = 4 GCF(8,4), since 4 goes into 8, GCF(8,4) = 4. GCF(28,8) = 4 Other examples:


Download ppt "Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed. 4-12-2018."

Similar presentations


Ads by Google