Download presentation

Presentation is loading. Please wait.

Published byAmia Webb Modified over 4 years ago

1
Lecture 1 -- 1Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the problem without recursion l A part that makes the problem simpler and then uses recursion rHere is a simple recursive function int factorial (int val) { cout << Entering with val = << val << endl; if (val == 1) {// simple part cout << Simple case << endl; return 1; { else {// simplification part int n = val * factorial (val-1); cout << Leaving n = << n << endl; return (n); }

2
Lecture 1 -- 2Computer Science I - Martin Hardwick Factorial 5 rAs we enter the function we print the current val rWhen we get to the simple case we print Simple case rAs we exit we print the value of n

3
Lecture 1 -- 3Computer Science I - Martin Hardwick The call stack rAs each function is entered a frame is added to the call stack rThe frame contains the values of the local variables rThe frame is created for both recursive and non-recursive functions CALL Stack: f3 () F3: f2 () F2: call f1 () F1: call return

4
Lecture 1 -- 4Computer Science I - Martin Hardwick The call stack for a recursive function rSame thing as the non-recursive case except that at some point the function (F3) must execute its simple case or the stack will overflow CALL Stack: F3 call F3 call F3 call F3 call return

5
Lecture 1 -- 5Computer Science I - Martin Hardwick The call stack and local variables rThe call stack stores the values of the function variables rEach frame in the call stack has its own copy rFor the factorial function this allows the first frame to have the value 4, the second to have the value 3, the third to have the value 2 and the last to have the value 1. Factorial: Val = 4 Factorial: Val = 3 Factorial: Val = 2 Factorial: Val = 1 N =Factorial (val -1) Stops because Val = 1 3 2 1

6
Lecture 1 -- 6Computer Science I - Martin Hardwick The call stack and return values rEach function call returns a value to the calling function using return rFor a recursive function this value holds the in-progress answer Factorial: N: Factorial: N: Factorial: N: Factorial: N: 1 2 6 1 2 6 24 Factorial (1) Factorial (2) Factorial (3) Main: :24 Factorial (4)

7
Lecture 1 -- 7Computer Science I - Martin Hardwick Recursive programming rThe hard part of recursive programming is recursive thinking. rYou have to learn to divide the program into: l A part that makes the problem simpler l A part that solves a simple case rConsider Pascals triangle How do we compute a value for row =5 Col = 2? The value of P[5][2] is P[4][1]+P[4][2] In general P[R][C] = P[R-1][C-1]+P[R-1][C] 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1

8
Lecture 1 -- 8Computer Science I - Martin Hardwick Pascal Triangle (continued) rWhat is the simple case l If (C = 0 || R == 0) then P[R][C] = 1. rSo our program is as follows if (C == 0 || R == 0) return 1 else return (p(R- 1, C- 1) + p(R – 1, C)); The value of P[5][2] is P[4][1]+P[4][2] 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1

9
Lecture 1 -- 9Computer Science I - Martin Hardwick Or is it? rWhat about P[4][4] l The value of P[3][4] is unknown! rSo our program is as follows if (C == 0 || R == C) return 1 else return (p(R- 1, C- 1) + p(R – 1, C)); 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 The value of P[5][2] is P[4][1]+P[4][2]

Similar presentations

OK

Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.

Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google