Finding largest integer in array

GreenZ
07-19-2003, 10:18 PM
I have a listbox which contains String values. There is duplicate strings in the list.
So I then search through the list and add how many times a particular string repeats itself and put that number into an array.

I need help to find the largest value in that array please.

*Also I set the array with the upper bound to some arbitrary number 100 because I wanted to set it to the list1.listcount value but it complained it needed to be set to a constant variable?

*Where can I find an algorithm to search for largest value in an array?


Thanks.

Optikal
07-20-2003, 12:00 AM
Dim i As Long
Dim lngMax As Long

For i = LBound(MyArray) To UBound(MyArray)
If MyArray(i) >= lngMax Then
lngMax = MyArray(i)
End If
Next i

phinds
07-20-2003, 06:54 AM
strictly speaking, Optikal's algorithm is incorrect because it will fail if the array is full of only negative numbers because he has failed to initialize lngMax to the biggest negative number in the long range and has just allowed the VB standard init to zero.

bpd
07-20-2003, 09:09 AM
strictly speaking, Optikal's algorithm is incorrect because it will fail if the array is full of only negative numbers because he has failed to initialize lngMax to the biggest negative number in the long range and has just allowed the VB standard init to zero.And so, a better example might be:Dim i As Long
Dim lngMax As Long

'To be able to re-size an array, the original declaration
'must be something like: Dim MyArray() As long
ReDim MyArray(1 To list1.ListCount) As Long

'Initialize to the 1st element.
lngMax = MyArray(LBound(MyArray))

'Try to find a larger element.
For i = LBound(MyArray) + 1 To UBound(MyArray)
If MyArray(i) >= lngMax Then
lngMax = MyArray(i)
End If
Next iHowever, this might also fail if there are no entries in the ListBox. I'll leave that as an exercise for the reader.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum