Results 1 to 6 of 6

Thread: Sorting numbers 1- 10

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    10

    Question

    I'm new to visual basic and can't figure out how to sort 10 numbers[the the user inputs in],find the high,the low,and the sum of all.



    If Num > then
    Print num
    If num < then
    Print num
    num = sum

    I'm really new at VB

    I can't remeber how to make a gui pop up and have the user input 10 numbers.Please help.

    Ron1jed
    Ummm now how do I do that?

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Here is a very simple example. it uses a bubble sort, which is a very simple sorting algorithim.

    You will need.

    ListBox called "List1"
    Text Box called "txtInput"
    Text Box called "txtSum"

    Command Button called "cmdAdd"
    Command Button called "cmdSort"
    Command Button called "cmdSum"

    Code:
    Option Explicit
    
    Dim iNumArray(1 To 10) As Integer
    Dim iNumberOfIntegers As Integer
    
    Private Sub cmdAdd_Click()
        
        'the number of numbers the user has entered
        iNumberOfIntegers = iNumberOfIntegers + 1
        
        'put it in the array
        iNumArray(iNumberOfIntegers) = Val(txtInput.Text)
        
        'add it the to the list box
        List1.AddItem Val(txtInput.Text)
        
    End Sub
    
    Private Sub cmdSort_Click()
        Dim i As Integer
        Dim iTemp As Integer
        Dim blswap As Boolean
        
        List1.Clear
        
        blswap = True
        Do While blswap = True
          'ahven't made a swap
          blswap = False
          
          'loop for the number of numbers -1
          For i = 1 To iNumberOfIntegers - 1
            'if the number at position i is greater than the
            'number at i+1 then swap them
            If iNumArray(i) > iNumArray(i + 1) Then
              iTemp = iNumArray(i)
              iNumArray(i) = iNumArray(i + 1)
              iNumArray(i + 1) = iTemp
              'mark as having made a swap
              blswap = True
            End If
          Next i
          
        Loop
              
        For i = 1 To iNumberOfIntegers
          List1.AddItem iNumArray(i)
        Next i
        
    End Sub
    
    Private Sub cmdSum_Click()
        Dim i As Integer
        Dim iSum As Long
        
        'loop to add the numbers
        For i = 1 To iNumberOfIntegers
          iSum = iSum + iNumArray(i)
        Next i
        
        txtSum.Text = iSum
        
    End Sub
    To find the highest and lowest values :

    Sort the list.
    Value 1 is the samllest.
    The last value is the highest.
    Iain, thats with an i by the way!

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    same deal..except popup inc

    the same as above except I used the Input box
    I assume that is the popup you were asking about.'general form..list1,list2 (Listboxes)
    'command1,command2 (Command Buttons)

    Option Explicit

    Private Sub Command1_Click()
    Call BubbleSortNumbers(iArray)
    Dim iLoop As Integer
    List2.addiem "Sorted Numbers:"
    List2.AddItem ""
    For iLoop = LBound(iArray) To UBound(iArray)
    List2.AddItem iArray(iLoop)
    Next iLoop
    Command1.Enabled = False
    End Sub

    Private Sub Command2_Click()

    Dim iCount As Integer
    Dim sNum
    List1.AddItem "Entered Numbers"
    List1.AddItem ""

    For iCount = 1 To 10
    sNum = InputBox("Enter your choice of a number!", "Enter Number")
    iArray(iCount) = sNum
    List1.AddItem iArray(iCount)
    Next iCount
    Command2.Enabled = False
    End Sub

    '=======================================================

    'add a bas module and put this code in it

    Option Explicit

    Public iArray(1 To 10) As Long

    '
    Public 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


    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    10
    Wow.You guys know your stuff.I remeber we just used a For loop.Our program was alot smaller.Thank you for the help.
    Ummm now how do I do that?

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    who here thinks ron1jed is having others do his homework for him, while posing with questions

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    10
    schools over for me ended Friday. Didn't learn much in computer scince.Our teacher made us copy programs 75 percent of the time.So i'm trying to figure out how they worked by trying them at home.
    Ummm now how do I do that?

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