min value in array

tinmanjo

Member
Licensed User
Is there a way to find the smallest/highest value in an array, say -4,2-10,-6,6,9 are a crude example of numbers.

Cant find a function for this, i know there is Arraylist.sort but this is for literal strings.

I need to find the smallest value in a large array.
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
If you don't need the array to be sorted then is fastest just to loop through the array. For example:

Sub Globals
Dim Arr (100) As Byte
End Sub

Sub Test

Dim r As Number
Dim btMin As Byte

btMin = Arr(0)

For r = 1 to ArrayLen(Arr()) - 1
If Arr(r) < btMin Then
btMin = Arr(r)
End If
Next r

End Sub


RBS
 

klaus

Expert
Licensed User
Longtime User
You must be careful with the variable declaration:
Dim Arr (100) As Byte

Byte is an unsigned integer !
But tinmanjo has also negative numbers.

The most suitable variable type with B4PPC 6.90 is Number, you could also use Integer, but B4PPC will convert the Integer variables to Number variables for the calculations.

Best regards.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…