Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays: Lists and Strings
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a...
-
date post
21-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a...
![Page 1: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/1.jpg)
1
Chapter 6
Looping
Dale/Weems/Headington
![Page 2: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/2.jpg)
2
Physical order vs. logical order A loop is a repetition control structure based on
a condition. it causes a single statement or block to be
executed repeatedly While loop
TRUE – execute the body, FALSE – exit loop Exits the loop only at the top
Errors: = vs. == Semicolon at end of While Braces
What is a loop?
![Page 3: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/3.jpg)
3
While Statement
SYNTAX
while ( Expression )
{ .
. // loop body
.
}
NOTE: Loop body can be a single statement, a null statement, or a block.
![Page 4: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/4.jpg)
4
Two Types of Loopscount controlled loops – executes a
number of timesloop control variable - initialize and test –
multiple roles
infinite loop – omit incrementation
repeat a specified number of times
event-controlled loops – termination depends on some event
some condition within the loop body changes and this causes the repeating to stop
![Page 5: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/5.jpg)
5
int count ;
count = 4; // initialize loop variable
while (count > 0) // test expression
{
cout << count << endl ; // repeated action
count -- ; // update loop variable
}
cout << “Done” << endl ;
Count-controlled Loop
![Page 6: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/6.jpg)
6
Infinite loops Other loops:
Do while For
Looping problems are difficult to solve
Easier to read than design Phases
Loop entry Iteration – before each iteration – loop
test After iteration – to statement following
loop
![Page 7: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/7.jpg)
7
myInfile contains 100 blood pressures
Use a while loop to read the 100 blood pressures and find their total
Count-Controlled Loop Example
![Page 8: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/8.jpg)
8
ifstream myInfile ;int thisBP ;int total ;int count ;
count = 0 ; // initialize
while ( count < 100 ) // test expression{ myInfile >> thisBP ;
total = total + thisBP ; count++ ; // update
}
cout << “The total = “ << total << endl ;
![Page 9: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/9.jpg)
9
Event-controlled Loops Sentinel controlled – long lists of data
keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop
End-of-file controlled
keep processing data as long as there is more data in the file
Flag controlled
keep processing data until the value of a flag changes in the loop body
![Page 10: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/10.jpg)
10
Examples of Kinds of Loops
Count controlled loop Read exactly 100 blood pressures from a file.
End-of-file controlledloop
Read all the blood pressures from a file no matter how many are there.
![Page 11: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/11.jpg)
11
Examples of Kinds of Loops
Sentinel controlled loop
Read blood pressures until a special value (like -1) selected by you is read.
Flag controlledloop
Read blood pressures until a dangerously high BP (200 or more) is read.
![Page 12: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/12.jpg)
12
A Sentinel-controlled Loop
requires a “priming read” – read before first test
“priming read” means you read one set of data before the while
Another read at end of loop
![Page 13: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/13.jpg)
13
// Sentinel controlled loop
total = 0;
cout << “Enter a blood pressure (-1 to stop ) ”;
cin >> thisBP;
while (thisBP != -1) // while not sentinel
{
total = total + thisBP;
cout << “Enter a blood pressure (-1 to stop ) ”;
cin >> thisBP;
}
cout << total;
![Page 14: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/14.jpg)
14
End-of-File Controlled Loop
depends on fact that a file goes into fail state when you try to read a data value beyond the end of the file
![Page 15: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/15.jpg)
15
total = 0;
myInfile >> thisBP; // priming read
while (myInfile) // while last read successful
{
total = total + thisBP;
myInfile >> thisBP; // read another
}
cout << total;
// End-of-file controlled loop
![Page 16: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/16.jpg)
16
//End-of-file at keyboard
total = 0;
cout << “Enter blood pressure (Ctrl-Z to stop)”;
cin >> thisBP; // priming read
while (cin) // while last read successful
{
total = total + thisBP;
cout << “Enter blood pressure”;
cin >> thisBP; // read another
}
cout << total;
![Page 17: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/17.jpg)
17
Flag-controlled Loops
you initialize a flag (to true or false) use meaningful name for the flag a condition in the loop body changes
the value of the flag test for the flag in the loop test
expression
![Page 18: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/18.jpg)
18
countGoodReadings = 0;
isSafe = true; // initialize Boolean flag
while (isSafe)
{
cin >> thisBP;
if ( thisBP >= 200 )
isSafe = false; // change flag value
else
countGoodReadings++;
}
cout << countGoodReadings << endl;
![Page 19: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/19.jpg)
19
Loops often used to
count all data values
count special data values
sum data values
keep track of previous and current values
![Page 20: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/20.jpg)
20
Previous and Current Values
write a program that counts the number of != operators in a program file
read one character in the file at a time
keep track of current and previous characters
![Page 21: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/21.jpg)
21
Keeping Track of Values
(x != 3) { cout << endl;}
FILE CONTENTS
previous current count
( x 0
! = 1
= ‘ ‘ 1
x ‘ ‘ 0
3 ) 1
‘ ‘ 3 1
‘ ‘ ! 0
![Page 22: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/22.jpg)
22
int count;
char previous;
char current;
count = 0 ;
inFile.get (previous); // priming reads
inFile.get(current);
while (inFile)
{ if ( (current == ‘=‘) && (previous == ‘!’) ) count++;
previous = current; // update
inFile.get(current); // read another
}
![Page 23: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/23.jpg)
23
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 24: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/24.jpg)
24
Patient Data
A file contains blood pressure data for different people. Each line has a patient ID, the number of readings for that patient, followed by the actual readings.
ID howMany Readings
4567 5 180 140 150 170 1202318 2 170 2105232 3 150 151 151
![Page 25: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/25.jpg)
25
4567 1522318 1905232 151 . . . . . .There were 432 patients in file.
Read the data and display a chart
Patient ID BP Average
![Page 26: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/26.jpg)
26
Algorithm Uses Nested Loops
initialize patientCount to 0 read first ID and howMany from file while not end-of-file
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 from file display patientCount
![Page 27: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/27.jpg)
27
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 28: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/28.jpg)
28
#include <iostream>#include <fstream>
using namespace std;
int main ( ){ int patientCount; // declarations int thisID; int howMany; int thisBP; int totalForPatient; int count;
float average;
ifstream myInfile;
![Page 29: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/29.jpg)
29
myInfile.open(“A:\\BP.dat”);
if (!myInfile ) // opening failed{ cout << “File opening error. Program terminated.”; return 1; }
cout << “ID Number Average BP” << endl;
patientCount = 0;
myInfile >> thisID >> howMany; // priming read
![Page 30: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/30.jpg)
30
while ( myInfile ) // last read successful{
patientCount++; cout << thisID; totalForPatient = 0; // initialize inner loop
count = 0; while ( count < howMany) { myInfile >> thisBP; count ++; totalForPatient = totalForPatient + thisBP; }
average = totalForPatient / float(howMany); cout << int (average + .5) << endl; // round myInfile >> thisID >> howMany; // another read}
![Page 31: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/31.jpg)
31
cout << “There were “ << patientCount << “patients on file.” << endl;
cout << “Program terminated.\n”;
return 0;
}
![Page 32: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/32.jpg)
32
Information About 20 Books in Diskfile
“A:\\myIn.dat”
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 33: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/33.jpg)
33
#include <iostream> // for cout#include <fstream> // for file I/O
using namespace std;
int main (void){ float price ; // declarations char kind ; ifstream myInfile ; float total = 0.0 ; int count = 1;
Program to Read Info about 20 Books From a Disk File
![Page 34: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/34.jpg)
34
Rest of Program
myInfile.open(“A:\\myIn.dat”) ;
// count-controlled processing loop while ( count <= 20 ) { myInfile >> price >> kind ;
total = total + price ; count ++ ;
} cout << “Total is: “ << total << endl ; myInfile.close( ) ; return 0 ;}
![Page 35: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/35.jpg)
35
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 36: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/36.jpg)
36
Complexity
is a measure of the amount of work involved in executing an algorithm relative to the size of the problem
![Page 37: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/37.jpg)
37
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 38: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/38.jpg)
38
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
![Page 39: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/39.jpg)
39
Program #2 - pg. 233 Input integers larger than 1 - loop for entry calculate sum of squares - 1 through that integer -
inner loop output results
declare variables initialize variables to 0 priming read-input while loop - while >1
– count = 1 - loop control variable - input - outer variable while count <= integer calculate square add to totaling variable add to counting variable
– output results input next number return 0
![Page 40: 1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.](https://reader033.fdocuments.in/reader033/viewer/2022051619/56649d605503460f94a41dae/html5/thumbnails/40.jpg)
40
Assignment #12 pg. 231 - prog warm-up exercises 1,4 pg. 233 - prog problems #2
Assignment #13 pg. 269-274 - exam prep exercises - #2-7,9-14.