Results 1 to 9 of 9

Thread: How do I display numbers back from least to greatest?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    6

    How do I display numbers back from least to greatest?

    I am in a programming class, and am one of the best students in it, I even went to a state programming contest. One of the problems was to enter 6 race times in a Console Application and have it display the times back in order of which were fastest and their place
    such as

    imput
    11
    15
    12
    8
    22
    18

    output
    8 1st
    11 2nd
    12 3rd
    15 4th
    18 5th
    22 6th

    I really dont have any idea where to start. So far I have this done

    Dim a1 As String
    Dim a2 As String
    Dim a3 As String
    Dim a4 As String
    Dim a5 As String
    Dim a6 As String


    Console.WriteLine("Enter Race Times")
    a1 = Console.ReadLine
    a2 = Console.ReadLine
    a3 = Console.ReadLine
    a4 = Console.ReadLine
    a5 = Console.ReadLine
    a6 = Console.ReadLine

    any suggestions I really need to figure this out.

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How do I display numbers back from least to greatest?

    This is VB6 or VB.NET? what's that Console Object?

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    6

    Re: How do I display numbers back from least to greatest?

    In school I'm using Vb.net. On my comp now im using vb6. Im using a console application instead of a windows application to make this. Heres the problem:

    Create a Console Application that inputs a list of times for a given track event.
    “Enter Times” …..
    Your application should put the times in order from fastest to slowest assigning a place 1st, 2nd, 3rd, 4th, 5th, 6th.
    Sample Output:
    9.7 1st
    10.2 2nd
    10.6 3rd
    10.7 4th
    11.3 5th
    11.4 6th

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How do I display numbers back from least to greatest?

    There are many easy ways to do it, but being the best in your class i'll give you a more "advanced" answer: What you need is a sorting algorithm, and there are many, each one performs better for particular situations. There are many threads about sorting, this one in the codebank by Ellis Dee is very detailed with useful information about most used sorting algorithms: LINK HERE
    Last edited by jcis; Apr 21st, 2009 at 10:13 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    6

    Re: How do I display numbers back from least to greatest?

    Ok I see what that is doing. One thing however. In my class we are having a mini competition. My teacher wants us to be able to code this with the least amount of code and use no references while in class. So what would be suggested as the easiest and simplest way to do from memory. As I said the sorting algorithm makes sense, but at the same time he wants this as simplistic as possible

  6. #6

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: How do I display numbers back from least to greatest?

    Insertion sort is the shortest algorithm from the perspective of lines of code. It's also probably the most efficient for short lists, but then again with only 6 items all algorithms will be super efficient.

    If you're thinking you don't want to use a sorting algorithm, you're getting lost in semantics. The very act of putting numbers in order is sorting, and if it's done via code, it's a sorting algorithm. So no matter what you're going to be using one. Whether you make one up from scratch, or cobble together something specifically to handle exactly 6 items, it's still a sorting algorithm.

    And pretty much no matter what you come up with, insertion sort will end up being fewer lines of code to accomplish the same task.

  8. #8
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Re: How do I display numbers back from least to greatest?

    Just create an array to store the six numbers. then pass the array to the following bubblesort (a slow sorting method)

    Code:
    Public Sub BubbleSort(ByRef arr As Variant, Optional numEls As Variant, Optional descending As Boolean)
        Dim value As Variant
        Dim Index As Long
        Dim firstItem As Long
        Dim indexLimit As Long, lastSwap As Long
        ' account for optional arguments
        If IsMissing(numEls) Then numEls = UBound(arr)
        firstItem = LBound(arr)
        lastSwap = numEls
        Do
            indexLimit = lastSwap - 1
            lastSwap = 0
            For Index = firstItem To indexLimit
                value = arr(Index)
                If (value > arr(Index + 1)) Xor descending Then
                    ' if the items are not in order, swap them
                    arr(Index) = arr(Index + 1)
                    arr(Index + 1) = value
                    lastSwap = Index
                End If
            Next
        Loop While lastSwap
    End Sub

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: How do I display numbers back from least to greatest?

    Quote Originally Posted by lok1234 View Post
    Code:
                If (value > arr(Index + 1)) Xor descending Then
    Very cool.

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