1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream> //std::cout , std::cin , std::streamsize
#include <vector> //std::vector<>
#include <limits> //std::numeric_limits<>
#include <algorithm> //std::minmax_element
#include <cmath> //pow()
int main( int argc , char **argv )
{
auto count = 0 , temp = 0 , average = 0;
long double total = 1; //could be a large total with multiplication and is a double because of sqrt
std::vector<unsigned short> age( 0 );
//unsigned since ages are postive.
//short because ages are less than like 32k
//or what ever the max on short is.
//( 0 ) because it starts as an empty vector
do
{
std::cout << "Please enter your age or <999> to quit.\n> " << std::flush;
std::cin >> temp;
while( std::cin.fail() )
//continue getting inputs while it is a bad input
{
std::cin >> temp;
//getting input
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n' );
//clearing buffer
}
if( temp != 999 )
{
age.push_back( temp );
}
//if the age is not the exit age then add it to the vector
++count;
} while( temp != 999 );
//count - 1 since I just incremented it.
count = 0;
for( const auto &it : age )
{
++count;
std::cout << "Person " << count << " is " << it << " year(s) old. " << std::endl;
total *= it;
average += it;
}
//iterate through the vector and output each person and their age.
std::cout << "The total amount of people entered is " << count << std::endl;
std::cout << "The average age is " << average / count << std::endl;
total = pow( total , ( double )1/count );
std::cout << "The Geometric mean is " << total << std::endl;
auto result = std::minmax_element( age.begin() , age.end() );
//getting the min and max value from the vector
std::cout << "The youngest person is person " << result.first-age.begin() + 1 << " and is " << *result.first << " year(s) old." << std::endl;
std::cout << "The oldest person is person " << result.second-age.begin() + 1 << " and is " << *result.second << " year(s) old." << std::endl;
//outputting the min age and max age
}
|
Please enter your age or <999> to quit.
> 1
Please enter your age or <999> to quit.
> 2
Please enter your age or <999> to quit.
> 3
Please enter your age or <999> to quit.
> 4
Please enter your age or <999> to quit.
> 5
Please enter your age or <999> to quit.
> 6
Please enter your age or <999> to quit.
> 7
Please enter your age or <999> to quit.
> 8
Please enter your age or <999> to quit.
> 9
Please enter your age or <999> to quit.
> 999
Person 1 is 1 year(s) old.
Person 2 is 2 year(s) old.
Person 3 is 3 year(s) old.
Person 4 is 4 year(s) old.
Person 5 is 5 year(s) old.
Person 6 is 6 year(s) old.
Person 7 is 7 year(s) old.
Person 8 is 8 year(s) old.
Person 9 is 9 year(s) old.
The total amount of people entered is 9
The average age is 5
The Geometric mean is 4.14717
The youngest person is person 1 and is 1 year(s) old.
The oldest person is person 9 and is 9 year(s) old.
Process returned 0 (0x0) execution time : 6.117 s
Press any key to continue.
|