5.10 LAB: Average and Maximum Values
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 average and max. A negative integer ends the input and is not included in the statistics. Write in c++
Ex: When the input is:
15 20 0 5 -1
the output is:
10 20
You can assume that at least one non-negative integer is input.
Ans
code:-
#include <iostream>
using namespace std;
int main()
{
int n,sum=0,c=0;//initialise
cin>>n;//input number
int max=n;
while(n>=0){//until number is non negative
sum=sum+n;c++;//update sum,count
if(n>max)//if max update max
max=n;
cin>>n;//input
cout<<(sum/c)<<" "<<max;
return 0;
}
If any doubt ask in the comments.
Get Answers For Free
Most questions answered within 1 hours.