Average of an array

Erel

B4X founder
Staff member
Licensed User
Longtime User
I was asked about calculating the average of values stored in an array.
So here is the answer:
B4X:
Sub Globals
    Dim values(0)
End Sub

Sub App_Start
    values() = Array(100,90,80,90,79,95)
    Msgbox(calcAverage)
End Sub
Sub calcAverage
    sum = 0
    For i = 0 To ArrayLen(values())-1
        sum = sum + values(i)
    Next
    Return sum / ArrayLen(values())
End Sub
 
Top