chapter 6 repetition

124
Computer Science: A Structured Programming Approach Using C 1 Objectives To understand basic loop concepts: pretest loops and post-test loops loop initialization and updating event and counter controlled loops To understand and be able to select the best loop construct for a given problem. To write programs that use the while, for, or do ... while statements. To understand the basic concepts and usage of recursion algorithms. To understand and be able to determine the efficiency of an algorithm through an analysis of its looping constructs. Chapter 6 Repetition

Upload: others

Post on 12-Apr-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 1

Objectives ❏ To understand basic loop concepts:

■ pretest loops and post-test loops

■ loop initialization and updating

■ event and counter controlled loops

❏ To understand and be able to select the best loop construct for a given problem.

❏ To write programs that use the while, for, or do ... while statements.

❏ To understand the basic concepts and usage of recursion algorithms.

❏ To understand and be able to determine the efficiency of an algorithm through

an analysis of its looping constructs.

Chapter 6

Repetition

Page 2: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 2

6-1 Concept of a loop

The real power of computers is in their ability to

repeat an operation or a series of operations many

times. This repetition, called looping, is one of the

basic structured programming concepts.

Each loop must have an expression that determines if

the loop is done. If it is not done, the loop repeats one

more time; if it is done, the loop terminates.

Page 3: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 3

FIGURE 6-1 Concept of a Loop

Page 4: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 4

6-2 Pretest and Post-test Loops

We need to test for the end of a loop, but where should

we check it—before or after each iteration? We can

have either a pre- or a post-test terminating condition.

In a pretest loop , the condition is checked at the

beginning of each iteration.

In a post-test loop, the condition is checked at the end

of each iteration.

Page 5: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 5

Pretest Loop

In each iteration, the control expression is tested first. If it is

true, the loop continues; otherwise, the loop is terminated.

Post-test Loop

In each iteration, the loop action(s) are executed. Then the

control expression is tested. If it is true, a new iteration is

started; otherwise, the loop terminates.

Note

Page 6: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 6

FIGURE 6-2 Pretest and Post-test Loops

Page 7: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 7

FIGURE 6-4 Minimum Number of Iterations in Two Loops

Page 8: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 8

6-3 Initialization and Updating

In addition to the loop control expression, two other

processes, initialization and updating, are associated

with almost all loops.

Loop Initialization

Loop Update

Topics discussed in this section:

Page 9: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 9

FIGURE 6-5 Loop Initialization and Updating

Page 10: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 10

6-4 Event- and Counter-Controlled

Loops

All the possible expressions that can be used in a loop

limit test can be summarized into two general categories:

event-controlled loops and counter-controlled loops.

Event-Controlled Loops

Counter-Controlled Loops

Loop Comparison

Topics discussed in this section:

6-4 Event- and Counter-Control

Page 11: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 11

FIGURE 6-7 Event-controlled Loop Concept

Page 12: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 12

FIGURE 6-8 Counter-controlled Loop Concept

Page 13: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 13

Table 6-1 Loop Comparisons

Page 14: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 14

6-5 Loops in C

C has three loop statements: the while, the for, and the

do…while. The first two are pretest loops, and the

the third is a post-test loop. We can use all of them

for event-controlled and counter-controlled loops.

The while Loop

The for Loop

The do…while Loop

The Comma Expression

Topics discussed in this section:

Page 15: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 15

FIGURE 6-9 C Loop Constructs

Page 16: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 16

FIGURE 6-10 The while Statement

Page 17: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 17

FIGURE 6-11 Compound while Statement

Page 18: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 18

PROGRAM 6-1 Process-control System Example

Page 19: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 6-2 A while Loop to Print Numbers

Page 20: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 20

PROGRAM 6-2 A while Loop to Print Numbers

Page 21: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 6-3 Adding a List of Numbers

Page 22: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 6-3 Adding a List of Numbers

Page 23: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 23

FIGURE 6-12 for Statement

Page 24: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 24

FIGURE 6-13 Compound for Statement

Page 25: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 25

A for loop is used when a loop is to be executed a known

number of times. We can do the same thing with a while

loop, but the for loop is easier to read and

more natural for counting loops.

Note

Page 26: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 26

FIGURE 6-14 Comparing for and while Loops

Page 27: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 27

PROGRAM 6-4 Example of a for Loop

Page 28: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 6-4 Example of a for Loop

Page 29: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 6-5 A Simple Nested for Loop

Page 30: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 6-5 A Simple Nested for Loop

Page 31: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 31

FIGURE 6-15 do…while Statement

Page 32: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 6-6 Two Simple Loops

Page 33: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 33

PROGRAM 6-6 Two Simple Loops

Page 34: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 34

FIGURE 6-16 Pre- and Post-test Loops

Page 35: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 6-7 Adding a List with the do…while

Page 36: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 36

PROGRAM 6-7 Adding a List with the do…while

Page 37: Chapter 6 Repetition

The Comma Expression

Complex expression made up of two expressions separated by a comma

Most often used in for statements

for (sum = 0, i = 1; i <= 20; i++) {

scanf(“%d”, &a);

sum += a;

} // for

Computer Science: A Structured Programming Approach Using C 37

Page 38: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 38

FIGURE 6-17 Nested Comma Expression

Page 39: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 39

PROGRAM 6-8 Comparison of while and do…while

Page 40: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 40

PROGRAM 6-8 Comparison of while and do…while

Page 41: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 41

6-6 Loop Examples

This section contains several short examples of loop

applications. Each program demonstrates one or more

programming concepts that you will find helpful in

solving other problems.

for Loops

while LOOPS

do…while LOOPS

Topics discussed in this section:

Page 42: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 42

PROGRAM 6-9 Compound Interest

Page 43: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 43

PROGRAM 6-9 Compound Interest

Page 44: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 44

PROGRAM 6-9 Compound Interest

Page 45: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 45

FIGURE 6-18 Print Right Triangle Flowchart and Pseudocode

Page 46: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 46

PROGRAM 6-10 Print Right Triangle Using Nested for

Page 47: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 47

PROGRAM 6-10 Print Right Triangle Using Nested for

Page 48: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 48

PROGRAM 6-11 Print Number Series Using User-specified Limit

Page 49: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 49

PROGRAM 6-11 Print Number Series Using User-specified Limit

Page 50: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 50

PROGRAM 6-12 Print Calendar Month

Page 51: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 51

PROGRAM 6-12 Print Calendar Month

Page 52: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 52

PROGRAM 6-12 Print Calendar Month

Page 53: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 53

Never use one variable to control two processes.

Note

Page 54: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 54

PROGRAM 6-13 Print Sum of Digits

Page 55: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 55

PROGRAM 6-13 Print Sum of Digits

Page 56: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 56

PROGRAM 6-14 Print Number Backward

Page 57: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 57

PROGRAM 6-14 Print Number Backward

Page 58: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 58

FIGURE 6-19 Design for Binary to Decimal

Page 59: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 59

PROGRAM 6-15 Convert Binary to Decimal

Page 60: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 60

PROGRAM 6-15 Convert Binary to Decimal

Page 61: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 61

PROGRAM 6-15 Convert Binary to Decimal

Page 62: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 62

PROGRAM 6-15 Convert Binary to Decimal

Page 63: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 63

PROGRAM 6-15 Convert Binary to Decimal

Page 64: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 64

PROGRAM 6-15 Convert Binary to Decimal

Page 65: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 65

PROGRAM 6-15 Convert Binary to Decimal

Page 66: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 66

6-7 Other Statements Related

to Looping

Three other C statements are related to loops: break,

continue, and goto. The last statements, the goto, is

not valid for structured programs and therefore is not

discussed in this text.

break

continue

Topics discussed in this section:

Page 67: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 67

FIGURE 6-20 break and Inner Loops

Page 68: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 68

PROGRAM 6-16 The for and while as Perpetual Loops

Page 69: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 69

PROGRAM 6-17 Using a break Flag

Page 70: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 70

FIGURE 6-21 The continue Statement

Page 71: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 71

PROGRAM 6-18 continue Example

Page 72: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 72

6-8 Looping Applications

In this section, we examine four common applications

for loops: summation, product, smallest and largest, and

inquiries. Although the uses for loops are virtually

endless, these problems illustrate many common

applications.

Summation

Powers

Smallest and Largest

Inquiries

Topics discussed in this section:

Page 73: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 73

FIGURE 6-22 Summation and Product Loops

Page 74: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 74

PROGRAM 6-19 Sum to EOF Function

Page 75: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 75

PROGRAM 6-19 Sum to EOF Function

Page 76: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 76

PROGRAM 6-20 Powers Function

Page 77: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 77

PROGRAM 6-20 Powers Function

Page 78: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 78

To find the sum of a series, the result is initialized to 0;

to find the product of a series, the result is initialized to 1.

Note

Page 79: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 79

FIGURE 6-23 Smallest and Largest Loops

Page 80: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 80

PROGRAM 6-21 Smallest to EOF Function

Page 81: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 81

PROGRAM 6-21 Smallest to EOF Function

Page 82: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 82

To find the largest, we need to initialize the smallest variable

to a very small number, such as INT_MIN.

To find the smallest, we need to initialize the result to a very

large number, such as INT_MAX.

Note

Page 83: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 83

FIGURE 6-24 any and all Inquiries

Page 84: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 84

PROGRAM 6-22 anyPositive to EOF Function

Page 85: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 85

PROGRAM 6-22 anyPositive to EOF Function

Page 86: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 86

PROGRAM 6-23 All Positive Function

Page 87: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 87

PROGRAM 6-22 anyPositive to EOF Function

Page 88: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 88

6-9 Recursion

In general, programmers use two approaches to

writing repetitive algorithms. One approach uses loops;

the other uses recursion. Recursion is a repetitive

process in which a function calls 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:

Page 89: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 89

FORMULA 6-1 Iterative Factorial Definition

Page 90: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 90

FORMULA 6-2 Recursive Factorial Definition

Page 91: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 91

FIGURE 6-25 Factorial (3) Recursively

Page 92: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 92

PROGRAM 6-24 Iterative Factorial Function

Page 93: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 93

PROGRAM 6-25 Recursive Factorial Function

Page 94: Chapter 6 Repetition

Designing Recursive Functions

Requirements of a recursive function

Call to itself with a reduced data size

Termination condition

Example

Factorial

Computer Science: A Structured Programming Approach Using C 94

Page 95: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 95

Every recursive call must either solve part of the problem

or reduce the size of the problem.

Note

Page 96: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 96

FIGURE 6-26 Calling a Recursive Function

Page 97: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 97

FIGURE 6-27 Fibonacci Numbers

Page 98: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 98

PROGRAM 6-26 Recursive Fibonacci

Page 99: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 99

PROGRAM 6-26 Recursive Fibonacci

Page 100: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 100

PROGRAM 6-26 Recursive Fibonacci

Page 101: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 101

Table 6-2 Fibonacci Run Time

Page 102: Chapter 6 Repetition

Limitations of Recursion

Recursive solutions may involve extensive overhead because of function calls

Each recursive call uses up some of memory allocation

Possibly duplicate computation, but not always

Computer Science: A Structured Programming Approach Using C 102

Page 103: Chapter 6 Repetition

Recursive vs. Iterative Solutions

Many algorithms are easier to implement recursively, while providing efficiency

Example

Towers of Hanoi

Computer Science: A Structured Programming Approach Using C 103

Page 104: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 104

FIGURE 6-28 Towers of Hanoi—Start Position

Page 105: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 105

FIGURE 6-29 Towers Solution for Two Disks

Page 106: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 106

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

Page 107: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 107

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

Page 108: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 108

PROGRAM 6-27 Towers of Hanoi

Page 109: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 109

PROGRAM 6-27 Towers of Hanoi

Page 110: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 110

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

Page 111: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 111

6-10 Programming Example—

The Calculator ProgramLet’s look at our calculator program one more time. In

Chapter 5, we gave users the capability of selecting

one of four options: add, subtract, multiply, or divide.

However, if users needed to make two calculations,

they had to run the program twice. We now add a loop

that allows users to make as many calculations as

needed.

Page 112: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 112

PROGRAM 6-28 The Complete Calculator

Page 113: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 113

PROGRAM 6-28 The Complete Calculator

Page 114: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 114

PROGRAM 6-28 The Complete Calculator

Page 115: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 115

PROGRAM 6-28 The Complete Calculator

Page 116: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 116

PROGRAM 6-28 The Complete Calculator

Page 117: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 117

6-11 Software Engineering

In this section, we discuss some software engineering

issues related to loops.

Loops in Structure Charts

Determining Algorithm Efficiency

Linear Loops

Logarithmic Loops

Nested Loops

Big-O Notation

Standard Measures of Efficiency

Topics discussed in this section:

Page 118: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 118

FIGURE 6-31 Structure Chart Symbols for Loops

Page 119: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 119

FIGURE 6-32 Structure Chart for Process

Page 120: Chapter 6 Repetition

Determining Algorithm Efficiency

Linear loopsfor (int i = 0; i < 1000; i++) {

BODY;

}

Logarithmic loopsi = 1;

while (i < 1000) {

BODY;

i *= 2;

}

Computer Science: A Structured Programming Approach Using C 120

Page 121: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 121

Table 6-4 Analysis of Multiply / Divide Loops

Page 122: Chapter 6 Repetition

Determining Algorithm Efficiency

Nested loops

Quadratic

Dependent quadratic

Linear logarithmic

Computer Science: A Structured Programming Approach Using C 122

Page 123: Chapter 6 Repetition

Big-O Notation

Keep the largest term in the function, and discard the others

f(n) = n2 + n O(f(n)) = O(n2)

Measure of efficiency

Count of the most frequent major operations

Big-O notation

Computer Science: A Structured Programming Approach Using C 123

Page 124: Chapter 6 Repetition

Computer Science: A Structured Programming Approach Using C 124

Table 6-5 Measures of Efficiency