1 lecture 16 chapter 6 looping dale/weems/headington

19
1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

1

Lecture 16

Chapter 6

Looping

Dale/Weems/Headington

Page 2: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

2

Chapter 6 Topics

While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control

Input Data Using a While Statement for Summing and

Counting Nested While Loops Loop Testing and Debugging

Page 3: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

3

Revision of Lecture 15

Define event controlled loop (Sentinel, EOF, Flag)

What is a priming read? How do you pass EOF by keyboard? What is the use for flag controlled loop? Write a EOF controlled loop in which you

count the number of data items and show the total

Page 4: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

4

initialize outer loop

while ( outer loop condition )

{ . . .

initialize inner loop

while ( inner loop condition )

{

inner loop processing and update

}

. . .}

Pattern of a Nested Loop

Page 5: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

5

Patient Data

A program reads blood pressure data for different people. For each patient ID, the number of readings for that patient, followed by the actual readings are given.

ID howMany Readings

4567 5 180 140 150 170 1202318 2 170 2105232 3 150 151 151

Page 6: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

6

4567 1522318 1905232 151 . . . . . .

Read the data and display a chart as below

Patient ID BP Average

Page 7: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

7

Algorithm Uses Nested Loops

initialize patientCount to 0 read first ID and howMany while not end-of-data

increment patientCount display ID use a count-controlled loop to read

and sum up this patient’s howMany BP’s

calculate and display average for patient

read next ID and howMany display patientCount

Page 8: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

8

To design a nested loop

begin with outer loop

when you get to where the inner loop appears, make it a separate module and come back to its design later

Page 9: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

9

#include <iostream>using namespace std;

int main ( ){ int patientCount; // declarations int thisID; int howMany; int thisBP; int totalForPatient; int count;

float average;

Page 10: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

10

cout << “ID Number Average BP” << endl;

patientCount = 0;

cin >> thisID >> howMany; // priming read

Page 11: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

11

while ( cin ) // last read successful{

patientCount++; cout << thisID; totalForPatient = 0; // initialize inner loop

count = 0; while ( count < howMany) { cin >> thisBP; count ++; totalForPatient = totalForPatient + thisBP; }

average = totalForPatient / float(howMany); cout << int (average + .5) << endl; // round cin >> thisID >> howMany; // another read}

Page 12: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

12

cout << “There were “ << patientCount << “patients on file.” << endl;

cout << “Program terminated.\n”;

return 0;

}

Page 13: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

13

Information About 20 Books

3.98 P <eoln>7.41 H <eoln>8.79 P <eoln>

.

.

.

Price of bookHardback orPaperback?

WRITE A PROGRAM TO FIND TOTAL VALUE OF ALL BOOKS

Page 14: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

14

#include <iostream> // for cout

using namespace std;

int main (void){ float price ; // declarations char kind ; float total = 0.0 ; int count = 1;

Program to Read Info about 20 Books

Page 15: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

15

Rest of Program

// count-controlled processing loop while ( count <= 20 ) { cin >> price >> kind ;

total = total + price ; count ++ ;

} cout << “Total is: “ << total << endl ; return 0 ;}

Page 16: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

16

Trace of Program Variables

count price kind total 0.0

1 3.98 ‘P’ 3.98

2 7.41 ‘H’ 11.39

3 8.79 ‘P’ 20.18

4 etc.

20

21 so loop terminates

Page 17: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

17

Complexity

is a measure of the amount of work involved in executing an algorithm relative to the size of the problem

Page 18: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

18

Polynomial Times

N N0 N1 N2 N3

constant linear quadratic cubic

1 1 1 1 1

10 1 10 100 1,000

100 1 100 10,000 1,000,000

1,000 1 1,000 1,000,000 1,000,000,000

10,000 1 10,000 100,000,000 1,000,000,000,000

Page 19: 1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington

19

Loop Testing and Debugging test data should test all sections of program

beware of infinite loops -- program doesn’t stop

check loop termination condition, and watch for “off-by-1” problem

use get function for loops controlled by detection of ‘\n’ character

use algorithm walk-through to verify pre- and postconditions

trace execution of loop by hand with code walk-through

use a debugger to run program in “slow motion” or use debug output statements