Problem · Array
Average or Zero
Learn this problemProblem statement
Given a numeric array table, return its arithmetic mean.
The arithmetic mean is the sum of all values divided by the number of values. If table is empty, return 0.
Function
average(table: double[]) → doubleExamples
Example 1
table = [2,4,6,8]return = 5.0The values sum to 20. Dividing by the 4 values gives 5.
Example 2
table = []return = 0.0The input is empty, so the required result is 0.
Example 3
table = [-1.5,2.5,5]return = 2.0The values sum to 6. Dividing by the 3 values gives 2.