-
Hi,
I have a label1 and a command1 on my form. When the user clicks command1, a random amount of numbers, from 1 to 7 numbers, in the range of 1 to 15, appear in my label1.
I use this code, and it works well, to an extent:
Private Sub Command1_Click()
choose 1 to 7 numbers
Dim sarray(1 To 7)
'range of numbers is 1 to 15
Dim iCheck7(1 To 15)
Dim a As Integer
Dim i As Integer
Dim iTemp As Integer
For a = 1 To 15
iCheck7(a) = a
Next
Randomize
For a = 1 To 1 + Int(Rnd * 7)
i = Int(Rnd * (16 - a)) + a
iTemp = iCheck7(a)
iCheck7(a) = iCheck7(i)
iCheck7(i) = iTemp
'include a "," at the end of eacher number
sarray(a) = iCheck7(a) & ", "
Next
'print the results to label1
Label1.Caption = sarray(1) & sarray(2) & sarray(3) & sarray(4) & _
sarray(5) & sarray(6) & sarray(7)
End Sub
The problem is I need to have the numbers printed in order of size.
For example, instead of 8,3,10,1, I want to get 1, 3, 8, 10,
Could anybody tell me how to do this?
And finally, how do I get rid of that last "," character?
So instead of 1, 3, 8, 10, I would get 1, 3, 8, 10
Thanks for any help!
-
Code:
'to sort and array by numeric value
'create a standard project
'add a bas module and put this code in it
'
Sub BubbleSortNumbers(iArray As Variant)
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As Long
For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
For lLoop2 = LBound(iArray) + 1 To lLoop1
If iArray(lLoop2 - 1) > iArray(lLoop2) Then
lTemp = iArray(lLoop2 - 1)
iArray(lLoop2 - 1) = iArray(lLoop2)
iArray(lLoop2) = lTemp
End If
Next lLoop2
Next lLoop1
End Sub
'
'''''''''''''''''''''''''''''
Private Sub Command1_Click()
choose 1 to 7 numbers
Dim sarray(1 To 7)
'range of numbers is 1 to 15
Dim iCheck7(1 To 15)
Dim a As Integer
Dim i As Integer
Dim iTemp As Integer
For a = 1 To 15
iCheck7(a) = a
Next
Randomize
For a = 1 To 1 + Int(Rnd * 7)
i = Int(Rnd * (16 - a)) + a
iTemp = iCheck7(a)
iCheck7(a) = iCheck7(i)
iCheck7(i) = iTemp
Next
call bubblesort(sarray)
'print the results to label1
Label1.Caption = sarray(1)& "," & sarray(2) & "," & sarray(3) & "," & sarray(4) & "," _
& sarray(5) & "," & sarray(6) & "," & sarray(7)
End Sub
take the , out of here and put it in the print statement
'include a "," at the end of eacher number
sarray(a) = iCheck7(a) & ", "
Next
[Edited by HeSaidJoe on 07-14-2000 at 07:56 AM]
-
you can do it by these 2 ways:
1) get a new 1 number each time until it is bigger than the last number chose.
2) put all of the numbers into a simple 1 demension array, and sort them by the "Bubble method".
with this method, you have to move all the time to a couple of number and if the number in left is bigger than the number in the right, switch between them. (using a temporary variable).
do it over and over n times until the array is sorted, and then just print it. simple, no?
-
</>...corrected version
Code:
'Please ignore the above posting by me
'as it is a useless piece of crap
'this works just fine....
Option Explicit
'sorry for the lack of comments
'at work and busy...
'
Public Sub BubbleSortNumbers(iArray As Variant)
Dim Loop1 As Long
Dim Loop2 As Long
Dim lTemp As Long
For Loop1 = UBound(iArray) To LBound(iArray) Step -1
For Loop2 = LBound(iArray) + 1 To Loop1
If iArray(Loop2 - 1) > iArray(Loop2) Then
lTemp = iArray(Loop2 - 1)
iArray(Loop2 - 1) = iArray(Loop2)
iArray(Loop2) = lTemp
End If
Next Loop2
Next Loop1
Label1.Caption = ""
Dim myStr As String
Label1.FontSize = "14"
For Loop2 = 1 To 7
myStr = iArray(Loop2)
'If Left(myStr, 1) = "," Then Left(myStr, 1) = ""
Label1.Caption = Label1.Caption & "," & myStr
Next
myStr = Label1.Caption
Dim myLen
myLen = Len(myStr)
'get rid of , on right side of string
If Right(myStr, 1) = "," Then myStr = Left(myStr, myLen - 1)
Dim myString As String
Dim iCount As Integer
myAt = False
myString = myStr
myLen = Len(myString)
'get rid of , on left side of string
For iCount = 1 To myLen
If Left(myString, 1) = "," Then
myString = Right(myString, (myLen - iCount))
End If
Next
Label1.Caption = myString
End Sub
'
'''''''''''''''''''''''''''''
Private Sub Command1_Click()
Dim sarray(1 To 7)
'range of numbers is 1 to 15
Dim iCheck7(1 To 15)
Dim a As Integer
Dim i As Integer
Dim iTemp As Integer
For a = 1 To 15
iCheck7(a) = a
Next
Randomize
For a = 1 To 1 + Int(Rnd * 7)
i = Int(Rnd * (16 - a)) + a
iTemp = iCheck7(a)
iCheck7(a) = iCheck7(i)
iCheck7(i) = iTemp
sarray(a) = iCheck7(a)
Next
Call BubbleSortNumbers(sarray)
End Sub
-
Thanks HeSaidJoe,
I tried your first bit of code but it didn't work. I changed the 'Call Bubblesort' to 'CallBubblesortnumbers' (I presume that's what you meant?), but it still didn't seem to put the numbers in any particular order.
Thanks for the second post. I deleted: myAt=false, and it worked fine.
Thanks again!