Case 1: Stack Diagram - PowerPoint PPT Presentation

About This Presentation
Title:

Case 1: Stack Diagram

Description:

... we are using the post-increment operator, count (index ) will always call count(0). Once this function returns, index is incremented. ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 10
Provided by: Cer8
Category:

less

Transcript and Presenter's Notes

Title: Case 1: Stack Diagram


1
Recursion Examples
Fundamentals of CS
2
Case 1 Code
/ Recursion Case 1 / include
ltstdio.hgt void count (int index) main ()
count (0) getchar() void count (int
index) printf ("d", index) if (index lt
2) count (index1)
3
Case 1 Stack Diagram
count(2) index2
count(1) index1
count(1) index1
count(1) index1
count(0) index0
count(0) index0
count(0) index0
count(0) index0
count(0) index0
Final Output 012
Main
Main
Main
Main
Main
Main
Main
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.
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.
4
Case 2 Code
/ Recursion Case 2 / include
ltstdio.hgt void count (int index) main ()
count (0) getchar() void count (int
index) if (index lt 2) count
(index1) printf ("d", index)
5
Case 2 Stack Diagram
count(2) index2
count(1) index1
count(1) index1
count(1) index1
count(0) index0
count(0) index0
count(0) index0
count(0) index0
count(0) index0
Final Output 210
Main
Main
Main
Main
Main
Main
Main
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
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.
6
Case 3 Code
/ Recursion Case 3 / include
ltstdio.hgt void count (int index) main ()
count (0) getchar() void count (int
index) if (index lt 2) count
(index) printf ("d", index)
7
Case 3 Stack Diagram
count(2) index2
count(1) index2
count(1) index2
count(1) index2
count(0) index1
count(0) index1
count(0) index1
count(0) index1
count(0) index1
Final Output 221
Main
Main
Main
Main
Main
Main
Main
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) count
(2) prints 2
Time 5 count (2) exits. count (2) is popped off
stack. count(1) prints 2
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.
8
Case 4 Code
/ Recursion Case 4 / include
ltstdio.hgt void count (int index) main ()
count (0) getchar() void count (int
index) if (index lt 2) count (index)
printf ("d", index)
9
Case 4 Stack Diagram
count(0) index1
count(0) index1
count(0) index1
count(0) index1
count(0) index1
count(0) index1
Final Output There is none! This is an
Infinite Loop
count(0) index1
count(0) index1
count(0) index1
count(0) index1
Main
Main
Main
Main
Main
Time 0 Stack is Empty
Time 4count (0) is called. count(0) is called
again..
Time 5count (0) is called. count(0) is called
again..
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..
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!
Write a Comment
User Comments (0)
About PowerShow.com