Answer to Question #304745 in C++ for Program Training

Question #304745

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
1
Expert's answer
2022-03-02T03:05:00-0500
#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();
}

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 the first!

Leave a comment

LATEST TUTORIALS
New on Blog