Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.

Similar presentations


Presentation on theme: "Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count."— Presentation transcript:

1

2 Recursion Examples Fundamentals of CS

3 Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count (int index) { printf ("%d", index); if (index < 2) count (index+1); }

4 Time 0: Stack is Empty Time 1: Main is called. Time 2: count (0) is called. count (0) prints “0”. Time 3: count(1) is called. count(1) prints “1” Time 4: count (2) is called. count (2) prints “2” Time 5: count (2) exits. count (2) is popped off stack. Main count(0) index=0 Main count(0) index=0 count(1) index=1 Main count(0) index=0 count(1) index=1 count(2) index=2 Case 1: Stack Diagram Main count(0) index=0 count(1) index=1 Main count(0) index=0 Main Time 6: count(1) exits. count (1) is popped off stack. Time 7: count(0) exits. count(0) is popped off stack. Time 8: Stack is Empty main() exits. main() is popped off stack. Final Output: 012

5 Case 2: Code /* Recursion: Case 2 */ #include void count (int index); main () { count (0); getchar(); } void count (int index) { if (index < 2) count (index+1); printf ("%d", index); }

6 Time 0: Stack is Empty Time 1: Main is called. Time 2: count (0) is called. Time 3: count(1) is called. Time 4: count (2) is called. count(2) prints “2” Time 5: count (2) exits. count (2) is popped off stack. count(1) prints “1” Main count(0) index=0 Main count(0) index=0 count(1) index=1 Main count(0) index=0 count(1) index=1 count(2) index=2 Case 2: Stack Diagram Main count(0) index=0 count(1) index=1 Main count(0) index=0 Main Time 6: count(1) exits. count (1) is popped off stack. count(0) prints “0” Time 7: count(0) exits. count(0) is popped off stack. Time 8: Stack is Empty main() exits. main() is popped off stack. Final Output: 210

7 Case 3: Code /* Recursion: Case 3 */ #include void count (int index); main () { count (0); getchar(); } void count (int index) { if (index < 2) count (++index); printf ("%d", index); }

8 Time 0: Stack is Empty Time 1: Main is called. Time 2: count (0) is called. index is incremented to 1. Time 3: count(1) is called. index is incremented to 2. Time 4: count (2) is called. count(2) prints “2” Time 5: count (2) exits. count (2) is popped off stack. count(1) prints “2” Main count(0) index=1 Main count(0) index=1 count(1) index=2 Main count(0) index=1 count(1) index=2 count(2) index=2 Case 3: Stack Diagram Main count(0) index=1 count(1) index=2 Main count(0) index=1 Main Time 6: count(1) exits. count (1) is popped off stack. count(0) prints “1” Time 7: count(0) exits. count(0) is popped off stack. Time 8: Stack is Empty main() exits. main() is popped off stack. Final Output: 221

9 Case 4: Code /* Recursion: Case 4 */ #include void count (int index); main () { count (0); getchar(); } void count (int index) { if (index < 2) count (index++); printf ("%d", index); }

10 Time 0: Stack is Empty Time 1: Main is called. Time 2: count (0) is called. count(0) is called again. Time 3: count (0) is called. count(0) is called again.. Time 4:count (0) is called. count(0) is called again.. Time 5:count (0) is called. count(0) is called again.. Main count(0) index=1 Main count(0) index=1 count(0) index=1 Main Case 4: Stack Diagram Main Final Output: There is none! This is an Infinite Loop count(0) index=1 count(0) index=1 count(0) index=1 count(0) index=1 count(0) index=1 count(0) index=1 count(0) index=1 Because we are using the post- increment operator, count (index++) will always call count(0). Once this function returns, index is incremented. But, this never happens as we have created an Infinite Loop!


Download ppt "Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count."

Similar presentations


Ads by Google