Results 1 to 11 of 11

Thread: [RESOLVED] Find matching top =3 in an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    25

    Resolved [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

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Find matching top =3 in an array

    Milk's logic in code:
    vb Code:
    1. Private Function GetThird(plngArray() As Long) As Long
    2.     Dim lngCompare As Long
    3.     Dim i As Long
    4.  
    5.     If UBound(plngArray) < 3 Then
    6.         ' If array has 3 or fewer items, return last item
    7.         GetThird = UBound(plngArray)
    8.     Else
    9.         ' Identify 3rd item in list
    10.         lngCompare = plngArray(2)
    11.         ' Starting with the 4th item, loop until we find
    12.         ' an item that isn't the same as the 3rd
    13.         For i = 3 To UBound(plngArray)
    14.             ' If this isn't the same as the 3rd item, we're done
    15.             If plngArray(i) <> lngCompare Then Exit For
    16.         Next
    17.         ' If there were no duplicate values, the loop
    18.         ' exited in the first iteration when i = 3. Since
    19.         ' there were no duplicates, we want to return
    20.         ' the third item, which is 2. (0, 1, 2)
    21.         ' Since 3 - 1 = 2, we can return i - 1
    22.  
    23.         ' If, however, there were duplicates, the loop
    24.         ' incremented the counter until we found an item
    25.         ' that didn't match the 3rd item. In this case, i is
    26.         ' now equal to the index of the first non-match.
    27.         ' We want to return the item right before the first
    28.         ' non-match, so we return i - 1.
    29.  
    30.         ' Finally, if ALL items beyond the third were the
    31.         ' same, we want to return the last item. When
    32.         ' For...Next loops end naturally, their counter
    33.         ' variable (i) is left with the value one greater
    34.         ' than their limit. In this case, that'd be
    35.         ' UBound(plngArray) + 1. Because the entire
    36.         ' list is tied for third, we want to return the
    37.         ' last item. Since i is one greater than the
    38.         ' last item, we can return i - 1.
    39.  
    40.         GetThird = i - 1
    41.     End If
    42. End Function
    Last edited by Ellis Dee; Jun 20th, 2009 at 09:43 AM.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    25

    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

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Find matching top =3 in an array

    Quote Originally Posted by zygon View Post
    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.)

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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)

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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?

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    25

    Resolved 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
  •  



Click Here to Expand Forum to Full Width