



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
The answers and follow-up for the looping exercises in chapter 6 of a programming textbook. It includes corrected code, output, and explanations for various looping concepts such as priming reads, sentinel values, and nested loops.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




n = 2; while (n < 15) { cout << n << ’ ’; n = n + 2; }
cin.get(inChar); while (inChar != ’\n’) { cout << inChar; cin.get(inChar); }
cin.get(letter); while (cin) . .
This input statement primes not only the outer loop but also the inner loop.
count = 0; sum = 0; scoreFile >> score; while (scoreFile) { sum = sum + score; count++; scoreFile >> score; } if (count > 0) average = float(sum) / float(count);
tenMinute++; if (tenMinute > 5) { cout << endl; tenMinute = 0; hour++; if (hour == 13) hour = 1; else if (hour == 12) am = !am; } if (hour == 1 && tenMinute == 0 && am) done = true; }
cout << "Number of strings containing ’e’: " << numWithE << endl; if (numStrings > 0) cout << "Percentage of strings containing ’e’: " << float(numWithE) / float(numStrings) * 100.0 << endl; totalStrings = totalStrings + numStrings; totalWithE = totalWithE + numWithE; inFile >> aString; } cout << endl; cout << "Total number of strings: " << totalStrings << endl; cout << "Total number of strings containing ’e’: " << totalWithE << endl; if (totalStrings > 0) cout << "Percentage of strings containing ’e’: " << float(totalWithE) / float(totalStrings) * 100.0 << endl;
int main() { char sex; // Coded ’F’ = female, ’M’ = male . . string fileName; // External name of file int badDataSets; // Number of erroneous data sets . . incFile >> sex >> amount; femaleCount = 0; femaleSum = 0.0; maleCount = 0; maleSum = 0.0; badDataSets = 0;
while (incFile) { cout << "Sex: " << sex << " Amount: " << amount << endl;
if (amount < 0.0) // Check for invalid salary { cout << " Bad data−−negative salary " << endl; badDataSets++; } else if (sex == ’F’) { femaleCount++; femaleSum = femaleSum + amount; } else if (sex == ’M’) { maleCount++; maleSum = maleSum + amount; } else // Reject invalid sex code {
cout << " Bad data−−invalid sex code " << endl; badDataSets++; } incFile >> sex >> amount; } if (femaleCount <= 0) cout << "No females" << endl; // Avoid division by zero else { femaleAverage = femaleSum / float(femaleCount); cout << "For " << femaleCount << " females, the average " << "income is " << femaleAverage << endl; } if (maleCount <= 0) cout << "No males" << endl; // Avoid division by zero else { maleAverage = maleSum / float(maleCount); cout << "For " << maleCount << " males, the average " << "income is " << maleAverage << endl; } cout << "No. of erroneous data sets: " << badDataSets << endl; return 0; }
a. Add the following #include directive:
#include
b. Add the following declarations:
int femaleHighest; // Highest income for females int femaleLowest; // Lowest income for females int maleHighest; // Highest income for males int maleLowest; // Lowest income for males
c. Add the following initializations:
femaleHighest = 0.0; femaleLowest = FLT_MAX; maleHighest = 0.0; maleLowest = FLT_MAX;
d. Modify the loop as follows:
while (incFile) { cout << "Sex: " << sex << " Amount: " << amount << endl; if (sex == ’F’) { femaleCount++; femaleSum = femaleSum + amount; if (amount > femaleHighest) femaleHighest = amount; if (amount < femaleLowest) femaleLowest = amount;