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
Printable View
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
VB Code:
Dim i As Integer For i = 0 to 2 intLB(i) = intClicks(i) Next i
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?Quote:
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
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?
Theres probably a nicer way, but how about something like this?
VB Code:
Dim num(2) As Integer Dim index As Integer For i = 0 To 2 For b = 0 To UBound(intClicks) If i = 0 Then If intClicks(b) > num(i) Then index = b num(i) = intClicks(b) End If Else If intClicks(b) > num(i) And intClicks(b) < num(i - 1) Then index = b num(i) = intClicks(b) End If End If Next b intLB(i) = index Next i
VB Code:
Dim intClicks(4) As Integer Dim intIndex As Integer Dim strParts() As String Dim intLB(2) As Integer intClicks(0) = 99 intClicks(1) = 3 intClicks(2) = 22 intClicks(3) = 9 intClicks(4) = 5 ' Add the values and their index to a sorted listbox. (The listbox can be hidden). The values are formatted so that ' different length numbers will sort properly. For intIndex = 0 To 4 lstSorted.AddItem Format(intClicks(intIndex), "00000000") & ":" & intIndex Next ' The high 3 indexes are in the last three rows of the listbox For intIndex = 2 To 4 ' Split the values at the ":" strParts = Split(lstSorted.List(intIndex), ":") intLB(intIndex - 2) = strParts(1) Next
Hehe, I love how there's so many DIFFERENT ways to solve the same prolem in VB. Makes life interesting :)
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:
Private Sub Command1_Click() Dim MyArray() As Integer, TestArray() As Integer Dim HighValue As Integer ReDim MyArray(2) ReDim TestArray(6) 'Set values for Test Array TestArray(0) = 34 TestArray(1) = 67 TestArray(2) = 134 '3rd Highest Value TestArray(3) = 89 TestArray(4) = 5000 '2nd Highest Value TestArray(5) = 2 TestArray(6) = 9000 'Highest Value 'Get HighestValue For i = 0 To UBound(TestArray) If TestArray(i) > MyArray(0) Then MyArray(0) = TestArray(i) Next MyArray(0) = HighValue HighValue = 0 'Get 2nd Highest Value For i = 0 To UBound(TestArray) If TestArray(i) > HighValue And TestArray(i) < MyArray(0) Then HighValue = TestArray(i) Next MyArray(1) = HighValue HighValue = 0 'Get 3rd Highest Value For i = 0 To UBound(TestArray) If TestArray(i) > HighValue And TestArray(i) < MyArray(0) And TestArray(i) < MyArray(1) Then HighValue = TestArray(i) Next MyArray(2) = HighValue 'Loop thru the values in the new Array For i = 0 To UBound(MyArray) MsgBox MyArray(i) Next End Sub
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.
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:
Private Sub Form_KeyPress(KeyAscii As Integer) Static Player1Score As Integer, Player2Score As Integer, Player3Score As Integer 'Player1 key was pressed If KeyAscii = 97 Then Player1Score = Player1Score + 1 lblPlayer1.Height = lblPlayer1.Height + 20 lblPlayer1.Top = lblPlayer1.Top - 20 'Check if in 1st place If Player1Score > Player2Score And Player1Score > Player3Score Then lblPlayer1.BackColor = vbRed End If 'Check if in 2nd place If (Player1Score > Player2Score And Player1Score < Player3Score) Or (Player1Score > Player3Score And Player1Score < Player2Score) Then lblPlayer1.BackColor = vbGreen End If 'Check if in 3rd place If Player1Score < Player2Score And Player1Score < Player3Score Then lblPlayer1.BackColor = vbBlack End If End If 'Player2 key was pressed If KeyAscii = 115 Then Player2Score = Player2Score + 1 lblPlayer2.Height = lblPlayer2.Height + 20 lblPlayer2.Top = lblPlayer2.Top - 20 'Check if in 1st place If Player2Score > Player1Score And Player2Score > Player3Score Then lblPlayer2.BackColor = vbRed End If 'Check if in 2nd place If (Player2Score > Player1Score And Player2Score < Player3Score) Or (Player2Score > Player3Score And Player2Score < Player1Score) Then lblPlayer2.BackColor = vbGreen End If 'Check if in 3rd place If Player2Score < Player1Score And Player2Score < Player3Score Then lblPlayer2.BackColor = vbBlack End If End If 'Player3 key was pressed If KeyAscii = 100 Then Player3Score = Player3Score + 1 lblPlayer3.Height = lblPlayer3.Height + 20 lblPlayer3.Top = lblPlayer3.Top - 20 'Check if in 1st place If Player3Score > Player2Score And Player3Score > Player1Score Then lblPlayer3.BackColor = vbRed End If 'Check if in 2nd place If (Player3Score > Player2Score And Player3Score < Player1Score) Or (Player3Score > Player1Score And Player3Score < Player2Score) Then lblPlayer3.BackColor = vbGreen End If 'Check if in 3rd place If Player3Score < Player2Score And Player3Score < Player1Score Then lblPlayer3.BackColor = vbBlack End If If Player1Score = 100 Then MsgBox "Player 1 wins!" If Player2Score = 100 Then MsgBox "Player 2 wins!" If Player3Score = 100 Then MsgBox "Player 3 wins!" End If End Sub
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.
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.
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.