Results 1 to 11 of 11

Thread: Comparing 3 values

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Exclamation Comparing 3 values

    I did read the forums but I didn't quite find anything that was dumbed down enough for me being new to VB and all.

    I have a simple question:

    If I wanted to write a program that compared 3 numeric values and placed them in sequence from 1st to 3rd place how would I do that? Could anyone give me a template or somewhere to start?

    Keep in mind that in my introductory class all I have to work with here is logical operators, relational operators, and select case. I haven't studied arrays or sorting at this point.

    Bottom line I was told to write a program for class that involves a race. Three individuals times are put into the form in seconds and I calculate the first, second, and third place. I started out doing the following:
    VB Code:
    1. If var1 < var2 And var1 < var3 then
    2.  If var2 < var3 then
    3. txt1st = var1
    4. txt2nd = var2
    5. txt3rd =var3

    Realizing that I would have to do this 27 times I figured there would HAVE to be an easier even beginner way to accomplish this task. Can any of you great indivduals help me out?

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Comparing 3 values

    You could make 3 seperate functions that take in 3 different parameters. Have one that returns the min value of the 3, have one that returns the middle value, and one that returns the max value.

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Comparing 3 values

    I am kind of following what your saying but not quite could you provide me with an example?

  4. #4

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Comparing 3 values

    Coded that is so I gather an idea what what your working with.

  5. #5
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425

    Re: Comparing 3 values

    Maybe you could use an ArrayList. add the values as integers and call the sort() method. Next you could loop through the array and retreive the values and place them in the textboxes...
    "Experience is something you don't get until just after you need it."

  6. #6
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Comparing 3 values

    Keep in mind that in my introductory class all I have to work with here is logical operators, relational operators, and select case. I haven't studied arrays or sorting at this point.
    Would be the nicest way to do it though.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Comparing 3 values

    its really pretty simple. You don't even need an array list, standard arrays can be sorted.

    This subroutine would sort values.

    VB Code:
    1. Public Sub OrderValues(ByRef Values() As Integer)
    2.         Array.Sort(Values)
    3.     End Sub

    this is code to show an example of that routine
    VB Code:
    1. Dim MyValues() As Integer = {70, 5, 13, 54}
    2.         For Each I As Integer In MyValues
    3.             MessageBox.Show(I.ToString)
    4.         Next
    5.         OrderValues(MyValues)
    6.         For Each I As Integer In MyValues
    7.             MessageBox.Show(I.ToString)
    8.         Next

  8. #8
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Comparing 3 values

    well kinda a pain to not being be able to use arrays for sorting. have you learn how to use do or for loops? If so I can help you get started.

    kleinma he hasn't learned how to use arrays yet.

  9. #9
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Comparing 3 values

    Quote Originally Posted by high6
    well kinda a pain to not being be able to use arrays for sorting. have you learn how to use do or for loops? If so I can help you get started.

    kleinma he hasn't learned how to use arrays yet.
    They're extremely simple. Much more simple than creating a convaluded for/next loop, especially if you have several numbers to sort.

    An array is the easiest, shortest way. And besides, he really SHOULD learn to learn how to use arrays at SOME point, the earlier the better. They're extremely useful.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Comparing 3 values

    Quote Originally Posted by high6
    kleinma he hasn't learned how to use arrays yet.
    he has now, hasn't he??

  11. #11
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Comparing 3 values

    You could do it without arrays (since you are adamant about not using them ), with a little more code... I believe the below code loops for a maximum of three times, depending on the initial order of the variables...
    VB Code:
    1. Dim Value1 As Integer = 1000
    2.         Dim Value2 As Integer = 100
    3.         Dim Value3 As Integer = 10
    4.         DoSort(Value1, Value2, Value3)
    5.         Dim Counter As Integer = 0 'counter just to see how many times it loops
    6.         Do Until Value1 < Value2 AndAlso Value2 < Value3
    7.             If Value1 > Value2 Then
    8.                 'switch em
    9.                 Dim Temp As Integer = Value2
    10.                 Value2 = Value1
    11.                 Value1 = Temp
    12.             Else
    13.                 If Value2 > Value3 Then
    14.                    'switch em
    15.                     Dim Temp As Integer = Value3
    16.                     Value3 = Value2
    17.                     Value2 = Temp
    18.                 End If
    19.             End If
    20.             Counter += 1
    21.         Loop
    22.         'From Lowest To Highest
    23.         MessageBox.Show(Value1.ToString & ", " & _
    24.                         Value2.ToString & ", " & _
    25.                         Value3.ToString & ", loop count: " & _
    26.                         Counter.ToString)
    Seems like a lot of code, but it really doesn't loop much at all...
    Last edited by gigemboy; Feb 14th, 2007 at 05:52 PM.

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