Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assuming a data file is correct (end of file is the only reason for input failur

ID: 3531770 • Letter: A

Question

Assuming a data file is correct (end of file is the only reason for input failure), explain how these two loops differ, and why one is more reliable than the other.

  count = 0;          // no data yet
sum = 0; // no sum yet
// read until end of file
while (!infile.eof)
{
cin >> datum;
count++; // one more item
sum += dataum; // add to sum
}
cout << "Average of " << count
<< " scores is " << sum/count << endl;
  count = 0;          // no data yet
sum = 0; // no sum yet
// read until failure
// (this loop test is false on failure)
while (cin >> datum)
{
count++; // one more item
sum += dataum; // add to sum
}
cout << "Average of " << count
<< " scores is " << sum/count << endl;

Explanation / Answer

first loop is correct

since in second loop you're taking input from file but cin takes input from console window.


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote