# Is there a way to get the average in an array of Integers (via the use of a standard method)?

[quote=155115:@Rami Hassan]what is

Return If(t>0,t/(u+1),0)
[/quote]

Technically, you don’t need to check for zero in the sum, but you should check Ubound to make sure it’s greater than -1 or you’ll be dividing by zero.

I doubt that Xojo is going to implement a new Math module as Karen suggested.
May be they have changed their mind in the last years, but I remember some emails I had with Geoff some years ago about Vector3D classes. They were going to be deprecated and later on eliminated.
I argued for these classes to remain because they were very useful in geometry, but finally I had to create my own class because my software used vectors extensively. So, if something existed and it was eliminated, I can imagine that it is much more difficult to create new classes.
But I wish I’m wrong. May be new frames is the solution…

Understood, thanks everyone!

[quote=155108:@Oliver Osswald]I would quickly write my own extends for array, for instance like this (add such function to a module):

Function avg(Extends ar() As Integer) As Double
Dim u As Integer = UBound(ar)
Dim t As Integer = 0

For i As Integer = 0 To u
t = t + ar(i)
Next

Return If(t>0,t/(u+1),0)
End Function

Which then can be used in code like this:

Dim numbers() As Integer
numbers = Array(1,2,3,4)

MsgBox "Average : " + Str(numbers.Avg)

[/quote]

``````// Just an adjustment to avoid some math errors and less overflows ;)
Function avg(Extends ar() As Integer) As Double
Dim n As Integer = UBound(ar)+1
Dim t As Int64 = 0 // Int64 to hold a larger number

For each v As Integer in ar
t = t + v
Next

Return If(n=0, CDbl("NaN"), t/n ) // div by zero gets NaN
End Function``````