Question

In: Computer Science

Statistics are often calculated with varying amounts of input data. Write a program that takes any...

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.

Solutions

Expert Solution

  • Since language is not mentioned so I have used for the program.
  • So basically here we are taking the inputs till we get any negative value. When we get any negative value, we compute the average, find max and terminate the program.
  • In the comment section of the program, I have explained the detailed logic.
  • Please read it carefully from starting and try to run this program on your system.

Please refer to the comments of the program for more clarity.

#include<bits/stdc++.h>
using namespace std;

int main()  // Starting of the main method
{
    int arr[100000];  // Declaring an array to store the incoming non-negative values
    //Here I have taken the maximum size of the array for safety

    // Here length represent the current length/size of the array
    int input, length = 0; // Initializing the length of the array as 0


    // Initialized an infinite loop. It will run till it gets a negative value
    while(1)
    {
        cin >> input;  // Taking number as input
        if(input<0)
        {
            // When number is negative, calculating the average, sum and breaking/terminating the program
            double average;
            int sum=0;

            // Initialized the max value with the first element of the array
            int max_value = arr[0];

            // Traversing the array
            for(int i=0; i<length; i++)
            {
                sum = sum + arr[i];

                // Finding the maximum value from the array
                if(arr[i] > max_value)
                {
                    max_value = arr[i];
                }
            }

            // Finding the average
            average = (double)sum / length;

            // Printing the average and the max
            cout << "\nAverage: " << average << endl;
            cout << "Max: " << max_value << endl;
            break;
        }
        else
        {
            // When number is non negative, storing the input to the array and increasing the size
            arr[length] = input;
            length++;
        }
    }
    return 0;
}

Sample Input-Output/Code-run:

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

Statistics are often calculated with varying amounts of input data. Write a program that takes any...
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. Can this be written in CORAL please!!
Statistics are often calculated with varying amounts of inputdata. Write a program that takes any...
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
in c++ pleaseStatistics are often calculated with varying amounts of inputdata. Write a program...
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 20You can assume that at least one non-negative integer is input.
Write a program that takes a date as input and outputs the date's season. The input...
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...
Write a batch program that takes and echoes input data one character at a time until...
Write a batch program that takes and echoes input data one character at a time until EOF is encountered and then prints a summary such as The 14 lines of text processed contained 20 capital letters, 607 lowercase letters, and 32 punctuation marks. Program: C
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. The code needs to be in Java
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT