for C++
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.
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.
#include <iostream>
using namespace std;
int main()
{
int s=0;
int n,m=-1,c=0;
double avg=0;
cout<<"Enter numbers:";
while(true){
cin>>n;//read number from user
if(n<0)//break loop if number is negative
break;
if(m<n)
m=n;//get the maximum number
s=s+n;//add number read to s
c++;
}
avg=s/c;//get average
cout<<avg<<" "<<m;//print result
return 0;
}
Screenshots:
The screenshots are attached below for reference.
Please follow them for output.
Please upvote my answer. Thank you.
Get Answers For Free
Most questions answered within 1 hours.