Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the max and average. A negative integer ends the input and is not included in the statistics. Assume the input contains at least one non-negative integer.
Output each floating-point value with two digits after the decimal point, which can be achieved by executing cout << fixed << setprecision(2); once before all other cout statements.
Ex: When the input is:
15 20 0 3 -1
the output is:
20 9.50
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector<int>vint;
int value;
float sum = 0;
float aver;
cout << "Please, enter any non-negative integers (negative - exit program):";
do
{
cin >> value;
if (value > 0)
{
vint.push_back(value);
}
}while(value>0);
vector<int>::iterator it;
int maxval = vint[0];
for (it = vint.begin(); it != vint.end(); it++)
{
sum += *it;
if (maxval < *it)
{
maxval = *it;
}
}
cout << "Output is: "<<endl;
cout << fixed << setprecision(2)<< maxval<<" "<< sum / vint.size();
}
Comments
Leave a comment