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:

Respuesta :

Answer:

I am writing the code in C++. Let me know if your want the program in some other programming language.

#include <iostream> // used to invoke input output functions

using namespace std; // used to identify objects like cout cin etc

int main() //start of the main function of the program

{  int number; // integer type variable storing numbers

  cin>>number; // reads numbers that user inputs

       int count = 0; // counts the total numbers entered

       int maximum = 0; // stores maximum number

       int sum = 0; // stores the total sum of numbers

       int average;  // stores average of the numbers

while (number >= 0) // loop checks if the number enters is greater than 0

   {count++; // increments value of count by 1

     sum = sum+number; // stores addition of each number with total

     maximum =  std::max(maximum, number);

// function to find the maximum of the input numbers

     cin>>number;  // keeps reading the entered numbers    }

  if (count == 0) // if value of count is 0

     { average = 0; } // value of average is 0

  else

    {average = sum/count;} // formula to calculate average

      cout<<average; // displays average of numbers

      cout<<" "; //space between average and maximum numbers

      cout<<maximum; } // displays maximum number

Explanation:

The while loop checks the condition that if the integer type number entered by the users are greater than 0. This means it checks that the numbers should be non- negative.

If the while condition is true then the program control enters the body of the loop.

In the first iteration the value of count is incremented by 1. The current value of count was 0 which now becomes 1. The sum variable stores the sum of the entered number and value in sum variable. The current value of sum is 0. This will be added to the value of number. Next the max function is used to find the maximum of the input numbers. It finds the maximum value between each input number and the current value of maximum. cin will keep reading input numbers as entered by the user and stores it in number variable.

The loop will break once the user enters a negative number.

Now the if condition checks if the value of count is equal to 0. This means it checks if there is no number entered. If its true then the value of average will be 0 and if it is false then the average is calculated by dividing the total sum of input numbers (stored in sum variable) with the total numbers entered by user (stores in count variable)

Lastly the values of maximum and average variables are displayed in the output.

The program illustrates the use of loops or iterative statements.

Loops and iterations are used to perform repetitive operations.

The program in Python, where comments are used to explain each line is as follows:

#This gets the first input

num = int(input())

#This initializes all variables to 0

sum = 0; count = 0; max = 0

#The following is repeated while the user input is not negative

while num >=0 :

   #This adds all user inputs

   sum += num

   #This increments the number of inputs

   count += 1

   #The following if condition determines the maximum

   if max < num:

       max = num

   #This gets another input

   num = int(input())

   

#This prints the average

print(sum/count)

#This prints the maximum

print(max)

The program is implemented using a while loop

Read more about similar programs at:

https://brainly.com/question/15287875