Results 1 to 10 of 10

Thread: Can anyone help me figure this out?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2001
    Location
    Somewhere out in space
    Posts
    24

    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?

  2. #2
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2001
    Location
    Somewhere out in space
    Posts
    24

    Unhappy 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

  4. #4
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2001
    Location
    Somewhere out in space
    Posts
    24

    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.

  6. #6
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    sorry about that heres an example

    place a listbox control on your form

    in the form load event add these lines

    VB Code:
    1. List1.visible = False
    2. List1.Sorted = True

    your sub gets changed to this
    VB Code:
    1. Private Sub startButton_Click()
    2. Dim myARR(0 To 9)
    3.  
    4. For i = 0 To 9
    5.      Randomize
    6.      myARR(i) = (Int(Rnd * 100) + 1)
    7.      List1.Additem(myARR(i))
    8. Next i
    9.  
    10. For i = 0 to 9
    11.      myARR(i) = List1.List(i)
    12.      Print myArr(i)
    13. next i
    14.  
    15. End Sub

    Try that out

  7. #7
    Addicted Member machine_wars's Avatar
    Join Date
    Dec 2000
    Location
    NC
    Posts
    147
    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.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2001
    Location
    Somewhere out in space
    Posts
    24

    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.

  9. #9
    Addicted Member machine_wars's Avatar
    Join Date
    Dec 2000
    Location
    NC
    Posts
    147
    The above code will do it.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jul 2001
    Location
    Somewhere out in space
    Posts
    24

    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
  •  



Click Here to Expand Forum to Full Width