31
Computer Science: A Structured Programming Approach Using C 1 6-9 Recursion In general, programmers use two approaches In general, programmers use two approaches to writing repetitive algorithms. One approach to writing repetitive algorithms. One approach uses loops; the other uses recursion. Recursion uses loops; the other uses recursion. Recursion is a repetitive process in which a function calls is a repetitive process in which a function calls itself. itself. Iterative and Recursive Definition Iterative and Recursive Solution Designing Recursive Functions Fibonacci Numbers Limitations of Recursion The Towers Of Hanoi Topics discussed in this section: Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Embed Size (px)

Citation preview

Page 1: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 1

6-9 Recursion

In general, programmers use two approaches to In general, programmers use two approaches to writing repetitive algorithms. One approach uses writing repetitive algorithms. One approach uses loops; the other uses recursion. Recursion is a loops; the other uses recursion. Recursion is a repetitive process in which a function calls itself.repetitive process in which a function calls itself.

Iterative and Recursive DefinitionIterative and Recursive SolutionDesigning Recursive FunctionsFibonacci NumbersLimitations of RecursionThe Towers Of Hanoi

Topics discussed in this section:Topics discussed in this section:

Page 2: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 2

FORMULA 6-1 Iterative Factorial Definition

Page 3: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 3

FORMULA 6-2 Recursive Factorial Definition

Page 4: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 4

FIGURE 6-25 Factorial (3) Recursively

Page 5: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 5

PROGRAM 6-24 Iterative Factorial Function

Page 6: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 6

PROGRAM 6-25 Recursive Factorial Function

Page 7: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 7

Every recursive call must either solve part of the problemor reduce the size of the problem.

NoteNote

Page 8: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 8

FIGURE 6-26 Calling a Recursive Function

Page 9: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 9

FIGURE 6-27 Fibonacci Numbers

Page 10: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 6-26 Recursive Fibonacci

Page 11: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 6-26 Recursive Fibonacci

Page 12: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 12

PROGRAM 6-26 Recursive Fibonacci

Page 13: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 13

Table 6-2 Fibonacci Run Time

Page 14: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 14

FIGURE 6-28 Towers of Hanoi—Start Position

Page 15: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 15

FIGURE 6-29 Towers Solution for Two Disks

Page 16: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 16

FIGURE 6-30 Towers of Hanoi Solution for Three Disks (Part I)

Page 17: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 17

FIGURE 6-30 Towers of Hanoi Solution for Three Disks (Part II)

Page 18: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 18

PROGRAM 6-27 Towers of Hanoi

Page 19: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 6-27 Towers of Hanoi

Page 20: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 20

Table 6-3 Tracing of Program 6-27, Towers of Hanoi

Page 21: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 21

6-10 Programming Example— The Calculator ProgramLet’s look at our calculator program one more time. In Let’s look at our calculator program one more time. In Chapter 5, we gave users the capability of selecting Chapter 5, we gave users the capability of selecting one of four options: add, subtract, multiply, or divide. one of four options: add, subtract, multiply, or divide. However, if users needed to make two calculations, However, if users needed to make two calculations, they had to run the program twice. We now add a loop they had to run the program twice. We now add a loop that allows users to make as many calculations as that allows users to make as many calculations as needed. needed.

Page 22: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 6-28 The Complete Calculator

Page 23: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 6-28 The Complete Calculator

Page 24: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 6-28 The Complete Calculator

Page 25: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 6-28 The Complete Calculator

Page 26: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 6-28 The Complete Calculator

Page 27: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 27

6-11 Software Engineering

In this section, we discuss some software engineering In this section, we discuss some software engineering issues related to loops. issues related to loops.

Loops in Structure ChartsDetermining Algorithm EfficiencyLinear LoopsLogarithmic LoopsNested LoopsBig-O NotationStandard Measures of Efficiency

Topics discussed in this section:Topics discussed in this section:

Page 28: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 28

FIGURE 6-31 Structure Chart Symbols for Loops

Page 29: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 29

FIGURE 6-32 Structure Chart for Process

Page 30: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 30

Table 6-4 Analysis of Multiply / Divide Loops

Page 31: Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms

Computer Science: A Structured Programming Approach Using C 31

Table 6-5 Measures of Efficiency