|
-
Jun 20th, 2009, 02:09 AM
#1
Thread Starter
Junior Member
[RESOLVED] Find matching top =3 in an array
I have an array with weights(in pounds).
I have already sorted the array so it is descending sequence (ie. Heaviest weight is in array(0), next heaviest in array(1) etc).
What I want is to find the last item of the 3 heaviest... and no it is not the first 3. The weights could be the same, so I want the 3 heaveist Equal weights.
So if my array was something like....
128(0)
126(1)
125(2)
125(3)
123(4)
122(5)
Then I would be looking to return 3
simarly if the list was something like this
128(0)
127(1)
127(2)
127(3)
127(4)
122(5)
then I would be looking to return 4
128(0)
128(1)
126(2)
125(3)
125(4)
122(5)
Would return 2
I am processing multiple arrays, in which each array could be of any size although each time I do know how big that particular array is
Any ideas out there ?
it is VB6 btw.
Thanks in advance
Bry
-
Jun 20th, 2009, 07:21 AM
#2
Re: Find matching top =3 in an array
Your question and examples are not that clear to me.
If I have interpreted the question correctly then the examples should return 3,5,4 not 3,4,2.
Do you want to return the highest index of the heaviest 3 differing weights?
Edit: Okay I think I understand now.
The function should always return 2 (3rd index) unless...- the array is smaller than 2 (return UBound.)
- the next item(s) are of equal weight to item(2) (return index to last of these equal items)
Last edited by Milk; Jun 20th, 2009 at 07:58 AM.
-
Jun 20th, 2009, 08:33 AM
#3
Re: Find matching top =3 in an array
Milk's logic in code:
vb Code:
Private Function GetThird(plngArray() As Long) As Long
Dim lngCompare As Long
Dim i As Long
If UBound(plngArray) < 3 Then
' If array has 3 or fewer items, return last item
GetThird = UBound(plngArray)
Else
' Identify 3rd item in list
lngCompare = plngArray(2)
' Starting with the 4th item, loop until we find
' an item that isn't the same as the 3rd
For i = 3 To UBound(plngArray)
' If this isn't the same as the 3rd item, we're done
If plngArray(i) <> lngCompare Then Exit For
Next
' If there were no duplicate values, the loop
' exited in the first iteration when i = 3. Since
' there were no duplicates, we want to return
' the third item, which is 2. (0, 1, 2)
' Since 3 - 1 = 2, we can return i - 1
' If, however, there were duplicates, the loop
' incremented the counter until we found an item
' that didn't match the 3rd item. In this case, i is
' now equal to the index of the first non-match.
' We want to return the item right before the first
' non-match, so we return i - 1.
' Finally, if ALL items beyond the third were the
' same, we want to return the last item. When
' For...Next loops end naturally, their counter
' variable (i) is left with the value one greater
' than their limit. In this case, that'd be
' UBound(plngArray) + 1. Because the entire
' list is tied for third, we want to return the
' last item. Since i is one greater than the
' last item, we can return i - 1.
GetThird = i - 1
End If
End Function
Last edited by Ellis Dee; Jun 20th, 2009 at 09:43 AM.
-
Jun 20th, 2009, 08:43 AM
#4
Re: Find matching top =3 in an array
I wanted to avoid declaring extra variables just for the fun of it:
Code:
Public Function TopThree(LA() As Long) As Long
' last index
TopThree = UBound(LA)
' is the index greater than 2?
If TopThree > 2 Then
' loop from index 2 to last - 1
For TopThree = 2 To TopThree - 1
' is the next one different?
If LA(TopThree) <> LA(TopThree + 1) Then Exit For
Next
' TopThree = index when Exit For happened, if no Exit For then TopThree = UBound(LA)
End If
End Function
One of these days...
Last edited by Merri; Jun 20th, 2009 at 09:41 AM.
Reason: Added comments
-
Jun 20th, 2009, 09:26 AM
#5
Thread Starter
Junior Member
Re: Find matching top =3 in an array
Thanks for that folks,
and yes milk - that should have been 5, my bad.
Could I ask that you both do a description of what your routines do, to be honest I don't understand the logic.
Thanks
Bry
-
Jun 20th, 2009, 09:38 AM
#6
Re: Find matching top =3 in an array
 Originally Posted by zygon
Could I ask that you both do a description of what your routines do, to be honest I don't understand the logic.
I added comments to mine; let me know if you have any questions.
Milk's code is just a more esoteric version of mine. (Which is based on his logic.)
-
Jun 20th, 2009, 09:42 AM
#7
Re: Find matching top =3 in an array
zygon, do you mean my first interpretation is correct? If so those two pieces of code are for something slightly different, they will return 3,4,2 (like your examples)
-
Jun 20th, 2009, 09:44 AM
#8
Re: Find matching top =3 in an array
I added comments to my previous post, but here is some additional comments as well. The code relies on how For loops work: they increment the given variable until it is greater than the range. If you do For lngA = 1 To 2 then the value of lngA at the end of the loop is 3. The only way to prevent this from happening is to exit the loop, which leaves the value of lngA at what it was when the exit happened.
-
Jun 20th, 2009, 09:47 AM
#9
Re: Find matching top =3 in an array
Aw, and after I added such nice comments. heh.
Which logic do you need? 3,5,4 or 3,4,2?
-
Jun 20th, 2009, 10:11 AM
#10
Re: Find matching top =3 in an array
I just go ahead and post both in one pasteable form:
Code:
Option Explicit
Public Function TopThree(LA() As Long) As Long
' last index
TopThree = UBound(LA)
' is the index greater than 2?
If TopThree > 2 Then
' loop from index 2 to last - 1
For TopThree = 2 To TopThree - 1
' is the next one different?
If LA(TopThree) <> LA(TopThree + 1) Then Exit For
Next
' TopThree = index when Exit For happened, if no Exit For then TopThree = UBound(LA)
End If
End Function
Public Function TopThree2(LA() As Long) As Long
Dim lngA As Long, lngB As Long
For lngA = 0 To UBound(LA) - 1
If LA(lngA) <> LA(lngA + 1) Then
lngB = lngB + 1
If lngB = 3 Then Exit For
End If
Next lngA
TopThree2 = lngA
End Function
Private Sub Form_Load()
Dim lngTest() As Long
ReDim lngTest(5)
lngTest(0) = 128
lngTest(1) = 126
lngTest(2) = 125
lngTest(3) = 125
lngTest(4) = 123
lngTest(5) = 122
Debug.Print TopThree(lngTest), TopThree2(lngTest)
lngTest(0) = 128
lngTest(1) = 127
lngTest(2) = 127
lngTest(3) = 127
lngTest(4) = 127
lngTest(5) = 122
Debug.Print TopThree(lngTest), TopThree2(lngTest)
lngTest(0) = 128
lngTest(1) = 128
lngTest(2) = 126
lngTest(3) = 125
lngTest(4) = 125
lngTest(5) = 122
Debug.Print TopThree(lngTest), TopThree2(lngTest)
End Sub
-
Jun 20th, 2009, 05:46 PM
#11
Thread Starter
Junior Member
Re: Find matching top =3 in an array
Ellis I would be looking to return 3 5 2 from my examples.
However the coding put in by merri appears to work fine
Thank you both very much
Added to Merris REP - You helped me before Ellis, wont let me add to yours, sorry.
I consider this thread resolved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|