Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Recursion Problem Solving & Program Design in C Sixth Edition.

Similar presentations


Presentation on theme: "© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Recursion Problem Solving & Program Design in C Sixth Edition."— Presentation transcript:

1 © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Recursion Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

2 1-2 © 2010 Pearson Addison-Wesley. All rights reserved. 1-2 Figure 10.1 Splitting a Problem into Smaller Problems

3 1-3 © 2010 Pearson Addison-Wesley. All rights reserved. 1-3 Figure 10.2 Recursive Function multiply

4 1-4 © 2010 Pearson Addison-Wesley. All rights reserved. 1-4 Figure 10.3 Thought Process of Recursive Algorithm Developer

5 1-5 © 2010 Pearson Addison-Wesley. All rights reserved. 1-5 Figure 10.4 Recursive Function to Count a Character in a String

6 1-6 © 2010 Pearson Addison-Wesley. All rights reserved. 1-6 Figure 10.5 Trace of Function multiply

7 1-7 © 2010 Pearson Addison-Wesley. All rights reserved. 1-7 Figure 10.6 Function reverse_input_words

8 1-8 © 2010 Pearson Addison-Wesley. All rights reserved. 1-8 Figure 10.7 Trace of reverse_input_words(3) When the Words Entered are "bits" "and" "bytes"

9 1-9 © 2010 Pearson Addison-Wesley. All rights reserved. 1-9 Figure 10.8 Sequence of Events for Trace of reverse_input_words(3)

10 1-10 © 2010 Pearson Addison-Wesley. All rights reserved. 1-10 Figure 10.9 Recursive Function multiply with Print Statements to Create Trace and Output from multiply(8, 3)

11 1-11 © 2010 Pearson Addison-Wesley. All rights reserved. 1-11 Figure 10.9 Recursive Function multiply with Print Statements to Create Trace and Output from multiply(8, 3) (contd)

12 1-12 © 2010 Pearson Addison-Wesley. All rights reserved. Why does recursion make it easier to conceptualize a solution? 1-12 You can use recursion in place of iteration (looping) One or more simple cases of the problem have a straightforward, non-recursive solution By applying this redefinition process every time the recursive function is called, eventually the problem is reduced to simple cases, which are relatievely easy to solve.

13 1-13 © 2010 Pearson Addison-Wesley. All rights reserved. The efficiency of recursion 1-13 As stated at the beginning of the chapter, recursive solutions are less efficient than an iterative solution in terms of computer time due to overhead for the extra function calls. However, in many instances the use of recursion enables us to specify a very natural, simple solution to a problem that would otherwise be very difficult to solve. It is a very important and powerful tool in problem solving and programming.

14 1-14 © 2010 Pearson Addison-Wesley. All rights reserved. Simple case vs. terminating condition 1-14 Simple cases involve simple solutions that can be split up into very small prblems A terminating condition may consist of an if statement that evaluates a particular condition, n <= 1. When the terminating condition is true, the function is dealing with one of the problems simple cases. The most common problem with a recursive function is that it may not terminate properly (in case the terminating condition is not correct or incomplete.

15 1-15 © 2010 Pearson Addison-Wesley. All rights reserved. 1-15 Figure 10.10 Recursive factorial Function

16 1-16 © 2010 Pearson Addison-Wesley. All rights reserved. 1-16 Figure 10.11 Trace of fact = factorial(3);

17 1-17 © 2010 Pearson Addison-Wesley. All rights reserved. 1-17 Figure 10.12 Iterative Function factorial

18 1-18 © 2010 Pearson Addison-Wesley. All rights reserved. 1-18 Figure 10.13 Recursive Function fibonacci

19 1-19 © 2010 Pearson Addison-Wesley. All rights reserved. 1-19 Figure 10.14 Program Using Recursive Function gcd

20 1-20 © 2010 Pearson Addison-Wesley. All rights reserved. 1-20 Figure 10.14 Program Using Recursive Function gcd (contd)

21 1-21 © 2010 Pearson Addison-Wesley. All rights reserved. 1-21 Figure 10.15 Recursive Function to Extract Capital Letters from a String

22 1-22 © 2010 Pearson Addison-Wesley. All rights reserved. 1-22 Figure 10.16 Trace of Call to Recursive Function find_caps

23 1-23 © 2010 Pearson Addison-Wesley. All rights reserved. 1-23 Figure 10.17 Sequence of Events for Trace of Call to find_caps from printf Statements

24 1-24 © 2010 Pearson Addison-Wesley. All rights reserved. 1-24 Figure 10.18 Trace of Selection Sort

25 1-25 © 2010 Pearson Addison-Wesley. All rights reserved. 1-25 Figure 10.19 Recursive Selection Sort

26 1-26 © 2010 Pearson Addison-Wesley. All rights reserved. 1-26 Figure 10.19 Recursive Selection Sort (contd)

27 1-27 © 2010 Pearson Addison-Wesley. All rights reserved. 1-27 Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings

28 1-28 © 2010 Pearson Addison-Wesley. All rights reserved. 1-28 Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (contd)

29 1-29 © 2010 Pearson Addison-Wesley. All rights reserved. 1-29 Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (contd)

30 1-30 © 2010 Pearson Addison-Wesley. All rights reserved. 1-30 Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (contd)

31 1-31 © 2010 Pearson Addison-Wesley. All rights reserved. 1-31 Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (contd)

32 1-32 © 2010 Pearson Addison-Wesley. All rights reserved. 1-32 Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (contd)

33 1-33 © 2010 Pearson Addison-Wesley. All rights reserved. 1-33 Figure 10.21 Towers of Hanoi

34 1-34 © 2010 Pearson Addison-Wesley. All rights reserved. 1-34 Figure 10.22 Towers of Hanoi After Steps 1 and 2

35 1-35 © 2010 Pearson Addison-Wesley. All rights reserved. 1-35 Figure 10.23 Towers of Hanoi After Steps 1, 2, 3.1, and 3.2

36 1-36 © 2010 Pearson Addison-Wesley. All rights reserved. 1-36 Figure 10.24 Recursive Function tower

37 1-37 © 2010 Pearson Addison-Wesley. All rights reserved. 1-37 Figure 10.25 Trace of tower ('A', 'C', 'B', 3);

38 1-38 © 2010 Pearson Addison-Wesley. All rights reserved. 1-38 Figure 10.26 Output Generated by tower ('A', 'C', 'B', 3);

39 1-39 © 2010 Pearson Addison-Wesley. All rights reserved. 1-39 Figure 10.27 Grid with Three Blobs


Download ppt "© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Recursion Problem Solving & Program Design in C Sixth Edition."

Similar presentations


Ads by Google