Results 1 to 14 of 14

Thread: Sorting Variables....HELP!

Hybrid View

  1. #1

    Thread Starter
    Addicted Member Illiad's Avatar
    Join Date
    Mar 2003
    Location
    Chicago
    Posts
    196

    Sorting Variables....HELP!

    I have an array of variables intClicks(4)
    I want the index's of the top 3 values to be put in an array intLB(2)

    both arrays start counting at 0 and are integers
    Last edited by Illiad; Jul 13th, 2003 at 10:56 PM.
    Quis Custodiet Ipsos Custodes?

    You don't have to be faster than the bear, you just have to be faster than the slowest person running from the bear.

  2. #2
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250
    VB Code:
    1. Dim i As Integer
    2. For i = 0 to 2
    3.     intLB(i) = intClicks(i)
    4. Next i
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  3. #3

    Thread Starter
    Addicted Member Illiad's Avatar
    Join Date
    Mar 2003
    Location
    Chicago
    Posts
    196
    I don't want the top 3 values of array intClicks in the array intLB, I want the top three value's indexes to go into the array intLB
    Quis Custodiet Ipsos Custodes?

    You don't have to be faster than the bear, you just have to be faster than the slowest person running from the bear.

  4. #4
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250
    Originally posted by Illiad
    I don't want the top 3 values of array intClicks in the array intLB, I want the top three value's indexes to go into the array intLB
    The index of the top 3 would be 0, 1 & 2? You want to put that into the other array?
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  5. #5

    Thread Starter
    Addicted Member Illiad's Avatar
    Join Date
    Mar 2003
    Location
    Chicago
    Posts
    196
    Ok, intclicks holds values like 100, 50, 75 ect. The index who's variable has the greatest value should be placed in intLB(0) second greatest's index should go in intLB(1) and third greatest needs to go in intLB(2)

    the indexes not the values need to go in intLB

    tricky huh?
    Quis Custodiet Ipsos Custodes?

    You don't have to be faster than the bear, you just have to be faster than the slowest person running from the bear.

  6. #6
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250
    Theres probably a nicer way, but how about something like this?
    VB Code:
    1. Dim num(2) As Integer
    2. Dim index As Integer
    3.  
    4. For i = 0 To 2
    5.     For b = 0 To UBound(intClicks)
    6.         If i = 0 Then
    7.             If intClicks(b) > num(i) Then
    8.                 index = b
    9.                 num(i) = intClicks(b)
    10.             End If
    11.         Else
    12.             If intClicks(b) > num(i) And intClicks(b) < num(i - 1) Then
    13.                 index = b
    14.                 num(i) = intClicks(b)
    15.             End If
    16.         End If
    17.     Next b
    18.     intLB(i) = index
    19. Next i
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Dim intClicks(4) As Integer
    2.     Dim intIndex As Integer
    3.     Dim strParts() As String
    4.     Dim intLB(2) As Integer
    5.    
    6.     intClicks(0) = 99
    7.     intClicks(1) = 3
    8.     intClicks(2) = 22
    9.     intClicks(3) = 9
    10.     intClicks(4) = 5
    11.    
    12.     ' Add the values and their index to a sorted listbox. (The listbox can be hidden). The values are formatted so that
    13.     ' different length numbers will sort properly.
    14.     For intIndex = 0 To 4
    15.         lstSorted.AddItem Format(intClicks(intIndex), "00000000") & ":" & intIndex
    16.     Next
    17.    
    18.     ' The high 3 indexes are in the last three rows of the listbox
    19.     For intIndex = 2 To 4
    20.         ' Split the values at the ":"
    21.         strParts = Split(lstSorted.List(intIndex), ":")
    22.         intLB(intIndex - 2) = strParts(1)
    23.     Next

  8. #8
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250
    Hehe, I love how there's so many DIFFERENT ways to solve the same prolem in VB. Makes life interesting
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  9. #9
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I'm working on an algorythm that does this in one loop of the Array but for now this will work. It requires looping thru the array 3 times..but assuming the array is small-ish like under a few thousand it shouldnt be a problem.

    here is an example in a click. It creates the inital array and loops thru the array at the end to show you the values it got... that why it looks so big.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim MyArray() As Integer, TestArray() As Integer
    3. Dim HighValue As Integer
    4. ReDim MyArray(2)
    5. ReDim TestArray(6)
    6. 'Set values for Test Array
    7. TestArray(0) = 34
    8. TestArray(1) = 67
    9. TestArray(2) = 134 '3rd Highest Value
    10. TestArray(3) = 89
    11. TestArray(4) = 5000 '2nd Highest Value
    12. TestArray(5) = 2
    13. TestArray(6) = 9000 'Highest Value
    14.  
    15. 'Get HighestValue
    16. For i = 0 To UBound(TestArray)
    17. If TestArray(i) > MyArray(0) Then MyArray(0) = TestArray(i)
    18. Next
    19. MyArray(0) = HighValue
    20. HighValue = 0
    21.  
    22. 'Get 2nd Highest Value
    23. For i = 0 To UBound(TestArray)
    24. If TestArray(i) > HighValue And TestArray(i) < MyArray(0) Then HighValue = TestArray(i)
    25. Next
    26. MyArray(1) = HighValue
    27. HighValue = 0
    28.  
    29. 'Get 3rd Highest Value
    30. For i = 0 To UBound(TestArray)
    31. If TestArray(i) > HighValue And TestArray(i) < MyArray(0) And TestArray(i) < MyArray(1) Then HighValue = TestArray(i)
    32. Next
    33. MyArray(2) = HighValue
    34.  
    35. 'Loop thru the values in the new Array
    36. For i = 0 To UBound(MyArray)
    37. MsgBox MyArray(i)
    38. Next
    39. End Sub
    Last edited by Arc; Jul 14th, 2003 at 12:39 AM.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  10. #10

    Thread Starter
    Addicted Member Illiad's Avatar
    Join Date
    Mar 2003
    Location
    Chicago
    Posts
    196
    Its not working. Maby if I explain my project a little more clearly. Its just a stupid game I made a long long time ago except im gonna enhance it. Its a bar graph race. You hit an assigned key as fast as you can while you race 4 other players to the top of the grid. If you win hurray!

    In this newer version I was going to have a leader board. The board would have 1st, 2nd, 3rd places depending on the number of clicks the person made. So when say the red bar would be in first lblLB(0) would read "Red" and its forecolor would change to red.
    Quis Custodiet Ipsos Custodes?

    You don't have to be faster than the bear, you just have to be faster than the slowest person running from the bear.

  11. #11
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    You mean like this? Cept i didn't use any arrays becasue there was no need and it would just make things harder


    3 labels on a form is all you need then paste this code

    VB Code:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2. Static Player1Score As Integer, Player2Score As Integer, Player3Score As Integer
    3.  
    4. 'Player1 key was pressed
    5. If KeyAscii = 97 Then
    6. Player1Score = Player1Score + 1
    7. lblPlayer1.Height = lblPlayer1.Height + 20
    8. lblPlayer1.Top = lblPlayer1.Top - 20
    9.   'Check if in 1st place
    10.   If Player1Score > Player2Score And Player1Score > Player3Score Then
    11.   lblPlayer1.BackColor = vbRed
    12.   End If
    13.   'Check if in 2nd place
    14.   If (Player1Score > Player2Score And Player1Score < Player3Score) Or (Player1Score > Player3Score And Player1Score < Player2Score) Then
    15.   lblPlayer1.BackColor = vbGreen
    16.   End If
    17.   'Check if in 3rd place
    18.   If Player1Score < Player2Score And Player1Score < Player3Score Then
    19.   lblPlayer1.BackColor = vbBlack
    20.   End If
    21. End If
    22. 'Player2 key was pressed
    23. If KeyAscii = 115 Then
    24. Player2Score = Player2Score + 1
    25. lblPlayer2.Height = lblPlayer2.Height + 20
    26. lblPlayer2.Top = lblPlayer2.Top - 20
    27.  
    28.  'Check if in 1st place
    29.   If Player2Score > Player1Score And Player2Score > Player3Score Then
    30.   lblPlayer2.BackColor = vbRed
    31.   End If
    32.   'Check if in 2nd place
    33.   If (Player2Score > Player1Score And Player2Score < Player3Score) Or (Player2Score > Player3Score And Player2Score < Player1Score) Then
    34.   lblPlayer2.BackColor = vbGreen
    35.   End If
    36.   'Check if in 3rd place
    37.   If Player2Score < Player1Score And Player2Score < Player3Score Then
    38.   lblPlayer2.BackColor = vbBlack
    39.   End If
    40.  
    41. End If
    42. 'Player3 key was pressed
    43. If KeyAscii = 100 Then
    44. Player3Score = Player3Score + 1
    45. lblPlayer3.Height = lblPlayer3.Height + 20
    46. lblPlayer3.Top = lblPlayer3.Top - 20
    47.  
    48.  'Check if in 1st place
    49.   If Player3Score > Player2Score And Player3Score > Player1Score Then
    50.   lblPlayer3.BackColor = vbRed
    51.   End If
    52.   'Check if in 2nd place
    53.   If (Player3Score > Player2Score And Player3Score < Player1Score) Or (Player3Score > Player1Score And Player3Score < Player2Score) Then
    54.   lblPlayer3.BackColor = vbGreen
    55.   End If
    56.   'Check if in 3rd place
    57.   If Player3Score < Player2Score And Player3Score < Player1Score Then
    58.   lblPlayer3.BackColor = vbBlack
    59.   End If
    60.  
    61. If Player1Score = 100 Then MsgBox "Player 1 wins!"
    62. If Player2Score = 100 Then MsgBox "Player 2 wins!"
    63. If Player3Score = 100 Then MsgBox "Player 3 wins!"
    64.  
    65. End If
    66.  
    67. End Sub
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  12. #12
    Lively Member
    Join Date
    Jul 2003
    Location
    Brisbane, Australia
    Posts
    72
    Here is a simple algorithm if you want the 3 greatest values from an Array. Sorry vb isn't my language of choice but this pseudocode should get the point across.

    int top3 [3] // Array of 3
    int yournumbers[50] // Array of 50
    int i
    int j = 0
    while j < 3 // Loop for the number of Maxs
    i = 0
    while (i < yournumber.length) // Loop for the number in the array
    if (yournumber[i] > top3[j]) then
    top3[j] = yournumbers[i]
    yournumbers[i] = 0 // Make sure it won't reappear
    end if
    i++ // increment i
    end while
    j++
    end while


    This code assumes an integer array initalises its values to 0. It will run with a complexity of N * M where N is the yournumbers array and M is the number of maxs your looking for.

  13. #13
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    That won't work. What if the highest number in the array is the very last number? Then the second and third numbers are not updated to reflect it.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  14. #14
    Lively Member
    Join Date
    Jul 2003
    Location
    Brisbane, Australia
    Posts
    72
    I re-read my code and it should work find for all cases.


    The reason it will (even when the highest is last) is that the first loop through looks for the highest number and puts that number in the first spot of the top3.

    The second time around it find the 2nd highest number and so on.

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