Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Lecture Computer 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 Computer 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 Computer 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 Computer 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 Computer 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 =

6 Lecture Computer 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: Factorial (1) Factorial (2) Factorial (3) Main: :24 Factorial (4)

7 Lecture Computer 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]

8 Lecture Computer 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]

9 Lecture Computer 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)); The value of P[5][2] is P[4][1]+P[4][2]


Download ppt "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."

Similar presentations


Ads by Google