|
-
Jul 18th, 2001, 12:27 AM
#1
Thread Starter
Junior Member
Can anyone help me figure this out?
I asked the same question today, and many nice folks gave me their opinions, thanx again, but I still have a problem solving it.
The idea is simple, but as a beginner it isn't all that easy to me.
So far I've managed to write this code,
Private Sub startButton_Click()
Dim myARR(0 To 9)
For i = 0 To 9
Randomize
myARR(i) = (Int(Rnd * 100) + 1)
Print myARR(i)
Next i
End Sub
, which will generate random number between 1 to 100 and display them on the form, but I have no idea how to sort them in numerical order. Can anyone help me about this?
-
Jul 18th, 2001, 12:36 AM
#2
Fanatic Member
forgive me for my having to ask this question but
If you are generating random numbers and displaying them on a form, and then you want to reorder the numbers back into numerical order. Doesnt that defeat the purpose of generating the numbers randomly?? Or is there events in between the generation and displaying of the numbers that needs to take place?
-
Jul 18th, 2001, 12:40 AM
#3
Thread Starter
Junior Member
Sorry for the poor explanation
What I wanna do is first to generate random numbers on the form, and then re-oder them numerially when I press a button.
e.g)
3
7
2
9
1
1
2
3
7
9
-
Jul 18th, 2001, 12:44 AM
#4
Fanatic Member
you could create a list box which is invisible set its sort property to true pass your values into the list box and then pass the ordered list box values back to the form to display
-
Jul 18th, 2001, 12:54 AM
#5
Thread Starter
Junior Member
Well...
I wish I could understand your advice.
Unfortunately, I am a complete newbie when it comes to VB, so I didn't really get your explanation.
Anyways, thank you for your reply.
-
Jul 18th, 2001, 01:01 AM
#6
Fanatic Member
sorry about that heres an example
place a listbox control on your form
in the form load event add these lines
VB Code:
List1.visible = False
List1.Sorted = True
your sub gets changed to this
VB Code:
Private Sub startButton_Click()
Dim myARR(0 To 9)
For i = 0 To 9
Randomize
myARR(i) = (Int(Rnd * 100) + 1)
List1.Additem(myARR(i))
Next i
For i = 0 to 9
myARR(i) = List1.List(i)
Print myArr(i)
next i
End Sub
Try that out
-
Jul 18th, 2001, 01:08 AM
#7
Addicted Member
ONE SMALL CHANGE
You can Bubblesort it
Code:
Option Explicit
Private Sub startButton_Click()
Dim myARR(0 To 9) As Integer
Dim i As Integer
For i = 0 To 9
Randomize
myARR(i) = (Int(Rnd * 100) + 1)
Print myARR(i)
Next i
Print vbCrLf
BubbleSort myARR
End Sub
Private Sub BubbleSort(theArray() As Integer)
Dim pass As Integer
Dim compare As Integer
Dim hold As Integer
For pass = 0 To UBound(theArray) - 1
For compare = 0 To UBound(theArray) - 1
If theArray(compare) > theArray(compare + 1) Then
hold = theArray(compare)
theArray(compare) = theArray(compare + 1)
theArray(compare + 1) = hold
End If
Next compare
Next pass
For pass = 0 To UBound(theArray)
Print theArray(pass)
Next pass
End Sub
Last edited by machine_wars; Jul 18th, 2001 at 01:16 AM.
-
Jul 18th, 2001, 01:11 AM
#8
Thread Starter
Junior Member
Wow!
OK, I'll try that one.
Uh... can I ask another one? actually, it's the same thing but...
Do you know how to do this without using 'listbox'?
I am wondering how to display those numbers on the form.
Thank you for you help, I really appreciate it.
-
Jul 18th, 2001, 01:21 AM
#9
Addicted Member
The above code will do it.
-
Jul 18th, 2001, 01:29 AM
#10
Thread Starter
Junior Member
Thanx a lot!
You helped me a lot, and I was able to learn very challenging stuffs for a few minutes, which could have taken days by myself .
Thank you again,
You guys Rock!
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
|