Results 1 to 6 of 6

Thread: printing numbers in ascending order (in a string)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    printing numbers in ascending order (in a string)

    Hi,

    My code below choses 10 numbers from 1 to 10 (same number not repeated twice).
    The numbers are then listed in my picturebox, but I need to sort them in ASCENDING ORDER.

    Any idea how I can do this?

    Thanks for any help!

    Public First As Integer, Second As Integer, Third As Integer, _
    Fourth As Integer, Fifth As Integer, Sixth As Integer, _
    Seventh As Integer, Eigth As Integer, Ninth As Integer, _
    Tenth As Integer

    Private Sub Command1_Click()
    Dim Numbers As New Collection, a As Integer
    Dim result As New Collection, temp As String

    Randomize

    Randomize
    For a = 1 To 10
    Numbers.Add a
    Next a
    Do While Numbers.Count
    a = Int(Rnd * Numbers.Count) + 1
    result.Add Numbers(a)
    Numbers.Remove a
    Loop

    First = result(1)
    Second = result(2)
    Third = result(3)
    Fourth = result(4)
    Fifth = result(5)
    Sixth = result(6)
    Seventh = result(7)
    Eigth = result(8)
    Ninth = result(9)
    Tenth = result(10)

    Picture1.Cls
    Picture1.Print First & " " & Second & " " & Third & " " & _
    Fourth & " " & Fifth & " " & Sixth & " " & _
    " " & Seventh & " " & Eigth & " " & Ninth & " " & Tenth
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Look up sorting in this forum.

    There are many ways you can sort, each have different techniques and are suitable for different sizes of arrays. Basically you want to find the largest item, then find the next, and then the next - so its all a matter of comparisons.

  3. #3
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    i think i have a problem with trying to figure out why people are trying to do something and spend less time trying to answer their question heh...

    but then i didnt understand the question so i ended up trying to figure out why again.

    why not just randomize 1 - 10 when your originally adding to the collection. and why use a collection instead of an integer array?

    What are you trying to sort by? the collections index or the value?

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Originally posted by Eyes.Only
    i think i have a problem with trying to figure out why people are trying to do something
    Consider it a valuable lesson, and well worth the effort!
    a) You will phrase your questions better when you need something (and not just on this forum).
    And b) When writing programs for clients / bosses / users etc they will also phrase their requirements in a non-logical manner, and you will need to translate what they asked for into what they actualy need.

  5. #5
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    haha very true

  6. #6
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    india
    Posts
    418
    hi,

    after added numbers try this

    for i=1 to 10
    for j=i+1 to 10
    if result(i)>result(j) then
    tmp=result(i)
    result(i)=result(j)
    result(j)=tmp
    end if
    next
    next.

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