



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Larry Caretto delivered this lecture in Computing in Engineering and Science course. Key points in this lecture are: Looping Code, File Names and Properties, Testing for End of File, File Buffering, Keyboard Error Test Loop, Trajectory Solution, Sentinel Solution
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Larry Caretto Computer Science 106
2
3
5
7
8
9
10
double x, xMin, xMax; int count = 0; ifstream inFile ( “input.dat” ); inFile >> x; xMin = x; xMax = x; while ( !infile.fail() ) { count++; if ( x > xMax ) xMax = x; else if ( x < xMin ) xMin = x; inFile >> x; }
11
12
19
20
ifstream inFile( “age.dat” ); double age, sum = 0; int n = 0; inFile >> age; while ( age >= 0 ) { sum += age; n++; inFile >> age; } if ( n > 0 ) cout << “The average age is “ << sum / n;
21
ifstream inFile( “age.dat” ); double age, sum = 0; int n = 0; do { inFile >> age; if ( age >= 0 ) { sum += age; n++; } } while ( age >= 0 ) if ( n > 0 ) cout << “The average age is “ << sum / n; 22
23
24
int first, last, i, sum = 0; cin << first << last; if ( first > last ) { int temp = first; first = last; last = temp; } if ( first % 2 != 0 ) first++; for ( i = first; i <= last; i += 2 ) sum += i;
25
if ( first % 2 != 0 ) first++;
for ( i = first; i <= last; i += 2 )
sum += i;