© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10:...

Post on 10-Dec-2015

218 views 0 download

Transcript of © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10:...

© 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

1-2

© 2010 Pearson Addison-Wesley. All rights reserved. 1-2

Figure 10.1 Splitting a Problem into Smaller Problems

1-3

© 2010 Pearson Addison-Wesley. All rights reserved. 1-3

Figure 10.2 Recursive Function multiply

1-4

© 2010 Pearson Addison-Wesley. All rights reserved. 1-4

Figure 10.3 Thought Process of Recursive Algorithm Developer

1-5

© 2010 Pearson Addison-Wesley. All rights reserved. 1-5

Figure 10.4 Recursive Function to Count a Character in a String

1-6

© 2010 Pearson Addison-Wesley. All rights reserved. 1-6

Figure 10.5 Trace of Function multiply

1-7

© 2010 Pearson Addison-Wesley. All rights reserved. 1-7

Figure 10.6 Function reverse_input_words

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"

1-9

© 2010 Pearson Addison-Wesley. All rights reserved. 1-9

Figure 10.8 Sequence of Events for Trace of reverse_input_words(3)

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)

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) (cont’d)

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.

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.

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 problem’s 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.

1-15

© 2010 Pearson Addison-Wesley. All rights reserved. 1-15

Figure 10.10 Recursive factorial Function

1-16

© 2010 Pearson Addison-Wesley. All rights reserved. 1-16

Figure 10.11 Trace of fact = factorial(3);

1-17

© 2010 Pearson Addison-Wesley. All rights reserved. 1-17

Figure 10.12 Iterative Function factorial

1-18

© 2010 Pearson Addison-Wesley. All rights reserved. 1-18

Figure 10.13 Recursive Function fibonacci

1-19

© 2010 Pearson Addison-Wesley. All rights reserved. 1-19

Figure 10.14 Program Using Recursive Function gcd

1-20

© 2010 Pearson Addison-Wesley. All rights reserved. 1-20

Figure 10.14 Program Using Recursive Function gcd (cont’d)

1-21

© 2010 Pearson Addison-Wesley. All rights reserved. 1-21

Figure 10.15 Recursive Function to Extract Capital Letters from a String

1-22

© 2010 Pearson Addison-Wesley. All rights reserved. 1-22

Figure 10.16 Trace of Call to Recursive Function find_caps

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

1-24

© 2010 Pearson Addison-Wesley. All rights reserved. 1-24

Figure 10.18 Trace of Selection Sort

1-25

© 2010 Pearson Addison-Wesley. All rights reserved. 1-25

Figure 10.19 Recursive Selection Sort

1-26

© 2010 Pearson Addison-Wesley. All rights reserved. 1-26

Figure 10.19 Recursive Selection Sort (cont’d)

1-27

© 2010 Pearson Addison-Wesley. All rights reserved. 1-27

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings

1-28

© 2010 Pearson Addison-Wesley. All rights reserved. 1-28

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

1-29

© 2010 Pearson Addison-Wesley. All rights reserved. 1-29

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

1-30

© 2010 Pearson Addison-Wesley. All rights reserved. 1-30

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

1-31

© 2010 Pearson Addison-Wesley. All rights reserved. 1-31

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

1-32

© 2010 Pearson Addison-Wesley. All rights reserved. 1-32

Figure 10.20 Recursive Set Operations on Sets Represented as Character Strings (cont’d)

1-33

© 2010 Pearson Addison-Wesley. All rights reserved. 1-33

Figure 10.21 Towers of Hanoi

1-34

© 2010 Pearson Addison-Wesley. All rights reserved. 1-34

Figure 10.22 Towers of Hanoi After Steps 1 and 2

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

1-36

© 2010 Pearson Addison-Wesley. All rights reserved. 1-36

Figure 10.24 Recursive Function tower

1-37

© 2010 Pearson Addison-Wesley. All rights reserved. 1-37

Figure 10.25 Trace of tower ('A', 'C', 'B', 3);

1-38

© 2010 Pearson Addison-Wesley. All rights reserved. 1-38

Figure 10.26 Output Generated by tower('A', 'C', 'B', 3);

1-39

© 2010 Pearson Addison-Wesley. All rights reserved. 1-39

Figure 10.27 Grid with Three Blobs