In: Computer Science
in 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.
Explanation:
n in the variable taking number, max variable contains the maximum number inputted, average variable sums the numbers and later divide it by count to get average of numbers.
While loop is used to stop execution on a negative number as input.
Code:
#include
using namespace std;
int main() return 0; Output:
{
int n, max = -1, count =0, average = 0;
cin>>n;
while(n>=0)
{
average = average+n;
if(n>max)
{
max = n;
}
count++;
cin>>n;
}
average = (float)average/count;
cout<
}