Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington."— Presentation transcript:

1

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

3 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to Control Input Data l Using a While Statement for Summing and Counting l Nested While Loops l Loop Testing and Debugging

4 3 Revision of Lecture 15 l Define event controlled loop (Sentinel, EOF, Flag) l What is a priming read? l How do you pass EOF by keyboard? l What is the use for flag controlled loop? l Write a EOF controlled loop in which you count the number of data items and show the total

5 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

6 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 howManyReadings 4567 5180 140 150 170 120 23182170 210 52323150 151 151

7 6 4567152 2318190 5232151.. Read the data and display a chart as below Patient ID BP Average

8 7 Algorithm Uses Nested Loops l initialize patientCount to 0 l read first ID and howMany l while not end-of-data n increment patientCount n display ID n use a count-controlled loop to read and sum up this patient’s howMany BP’s n calculate and display average for patient n read next ID and howMany l display patientCount

9 8 To design a nested loop l begin with outer loop l when you get to where the inner loop appears, make it a separate module and come back to its design later

10 9 #include using namespace std; int main ( ) { int patientCount; // declarations int thisID; int howMany; int thisBP; int totalForPatient; int count; float average;

11 10 cout > thisID >> howMany; // priming read

12 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 }

13 12 cout << “There were “ << patientCount << “patients on file.” << endl; cout << “Program terminated.\n”; return 0; }

14 13 Information About 20 Books 3.98 P 7.41 H 8.79 P. Price of book Hardback or Paperback? WRITE A PROGRAM TO FIND TOTAL VALUE OF ALL BOOKS

15 14 #include // 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

16 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 ; }

17 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

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

19 18 Polynomial Times N N 0 N 1 N 2 N 3 constant linear quadratic cubic 11 1 1 1 101 10 100 1,000 1001 100 10,000 1,000,000 1,0001 1,000 1,000,000 1,000,000,000 10,0001 10,000 100,000,000 1,000,000,000,000

20 19 Loop Testing and Debugging test data should test all sections of program l beware of infinite loops -- program doesn’t stop l check loop termination condition, and watch for “off-by-1” problem l use get function for loops controlled by detection of ‘\n’ character l use algorithm walk-through to verify pre- and postconditions l trace execution of loop by hand with code walk-through l use a debugger to run program in “slow motion” or use debug output statements


Download ppt "1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington."

Similar presentations


Ads by Google