Calculating minimum, maximum, and average of inputted numbers

后端 未结 5 625
醉酒成梦
醉酒成梦 2021-01-07 11:54

Is there a way to calculate the average/min/max of all numbers without using an array? Need to calculate up to 10,000 numbers per sec.

相关标签:
5条回答
  • 2021-01-07 12:20

    Yes,

    Keep a minimum variable that is initialized to a high value, and update it if you see a lower value.

    Do the opposite with a maximum variable.

    Add up all numbers and divide that sum by the total count to get the average.

    The following code does not do bounds checks (e.g. count > 0, total doesn't overflow) but should give you an idea:

    int minimum = // Initialize to large #, in C# would be int.MaxValue
    int maximum = // Initialize to most negative #, in C# would be int.MinValue
    int count = 0;
    int total = 0;
    
    void StatsForNewNumber(int number)
    {
        if (number < minimum) minimum = number;
        if (number > maximum) maximum = number;
        count++;
        total += number;
    }
    
    int Average()
    {
        return total / count;
    }
    
    0 讨论(0)
  • 2021-01-07 12:28

    Sure. Keep the smallest and largest numbers you've received, along with the sum and count of numbers. When you need the smallest or largest, return it. When you need the average, divide the sum by the number.

    Boost Accumulators includes implementations of all the above, plus quite a few others.

    0 讨论(0)
  • 2021-01-07 12:30

    Create four variables: one to store the minVal, one for the maxVal, one for the total sum, and one to increment after each new input. compare each new input against minVal and maxVal and update as necessary. Add the input value to the total sum, increment the counter. The average is always the total sum/counter, so you can query this value on the fly if you need to or just calculate it at the end when you're done.

    0 讨论(0)
  • 2021-01-07 12:32

    You don't really need to store any numbers in an array to find the average/min/max, as you are iterating through the numbers you do

    if(currentSmallest > currentNumber)
         currentSmallest = currentNumber
    
    if(currentLargest < currentNumber)
         currentLargest = currentNumber
    

    and in addition you will keep a counter and the total sum, and by dividing those numbers you will get the average. No need to store them in an array.

    0 讨论(0)
  • 2021-01-07 12:35

    Absolutely: all that can be computed one item at a time.

    Keep the current minimum and the current maximum, compute the running total, and the count. When you need the average, divide the running total by the count, and you'll get your answer.

    class calc {
        double minVal, maxVal, total;
        int count;
    public:
        calc()
        :   minVal(numeric_limits<double>::max)
        ,   maxVal(numeric_limits<double>::min)
        ,   total(0)
        ,   count(0) {
        }
        void process(double num) {
            minVal = min(minVal, num);
            maxVal = max(maxVal, num);
            total += num;
            count++;
        }
        double getAvg() {
            // TODO: Check count to be > 0 here
            return total / count;
        }
        double getMin() {
            return minVal;
        }
        double getMax() {
            return maxVal;
        }
    }
    
    0 讨论(0)
提交回复
热议问题