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

Question #304747

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:05-0500
#include <bits/stdc++.h> 
#include<vector>
using namespace std;


/*
	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.
*/
int main()
{
	float c=0,temp=0,Max=0,Avg=0;
	c=0;
	cout<<"\n\eEnter Values separated by space: ";
	while(temp>=0)
	{
		cin>>temp; 
		if(temp>=0)
		{
			Avg = Avg + temp;
			if(Max<=temp) Max = temp;			
			c++;
		}
	}
	Avg = Avg/c;
	cout<<"\n\tMax. Value = "<<fixed<<setprecision(2)<<Max;
	cout<<"\n\tAverage    = "<<fixed<<setprecision(2)<<Avg;
	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 the first!

Leave a comment

LATEST TUTORIALS
New on Blog