Ask Question

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:

+3
Answers (1)
  1. 1 September, 06:40
    0
    I am writing the code in C++. Let me know if your want the program in some other programming language.

    #include / / 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<
    cout<<" "; / /space between average and maximum numbers

    cout<
    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers