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

// IntegerAverage.cpp // Calculate the average of several integers. #include <io

ID: 3627000 • Letter: #

Question

// IntegerAverage.cpp
// Calculate the average of several integers.

#include <iostream>
using namespace std;

int main()
{
int value; // current value
int count = 0; // number of inputs
int total; // sum of inputs

// prompt for input
cout << "Enter integers (9999 to end):" << endl;
cin >> value;

// loop until sentinel value read from user
/* Write a for header to initialize total to 0
and loop until value equals 9999 */
{
/* Write a statement to add value to total */
/* Write a statement to increment count */

cin >> value; // read in next value
} // end for

// if user entered at least one value
if ( count != 0 )
cout << " The average is: "
<< /* Convert total to a double and divide it by count */ << endl;
else
cout << " No values were entered." << endl;
} // end main

Explanation / Answer

// IntegerAverage.cpp
// Calculate the average of several integers.

#include <iostream>
using namespace std;

int main()
{
int value; // current value
int count = 0; // number of inputs
int total; // sum of inputs

// prompt for input
cout << "Enter integers (9999 to end):" << endl;
cin >> value;
total = 0;
// loop until sentinel value read from user
/* Write a for header to initialize total to 0
and loop until value equals 9999 */
while(value!=9999)
{
/* Write a statement to add value to total */
total = total + value;

/* Write a statement to increment count */
count++;
cin >> value; // read in next value
} // end for

// if user entered at least one value
if ( count != 0 )
cout << " The average is: " << static_cast<double> (total)/count << endl;
else
cout << " No values were entered." << endl;
} // end main