Calculating minimum, maximum, and average of inputted numbers

后端 未结 5 647
醉酒成梦
醉酒成梦 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: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.

提交回复
热议问题