63 152
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #58493 in C++ for atish

Question #58493
Make a program that will read an unknown number of ages. (0 is a valid age used for a baby.) The user will enter a negative number if there are no more ages to enter. The program will output the average age and youngest and oldest ages. If no ages were entered, display “No ages were entered” .
Expert's answer
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
// declaration and initialization of variables
int age, max_age = 0, min_age = 100, count_age = 0, summ_age = 0;
while (1) {
// value input
cout << endl << "Input age (negative number for stop input) - ";
cin >> age;
if ( age < 0 ) break; // exit of the loop if input a negative value
count_age++; // the number of input values count
summ_age += age; // calculation of the amount of the entered numbers
if (min_age > age) min_age = age; // definition of the minimum age
if (max_age < age) max_age = age; // definition of the maximum age
}
// output the average age and youngest and oldest ages
if ( count_age == 0 ) cout << "No ages were entered" ;
else cout << "Oldest age = " << max_age << endl << "Youngest age = " << min_age << endl << "Average age = " << summ_age / count_age ;
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions