|
-
Feb 14th, 2007, 08:02 AM
#1
Thread Starter
Addicted Member
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:
If var1 < var2 And var1 < var3 then
If var2 < var3 then
txt1st = var1
txt2nd = var2
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?
-
Feb 14th, 2007, 08:14 AM
#2
Fanatic Member
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.
-
Feb 14th, 2007, 08:17 AM
#3
Thread Starter
Addicted Member
Re: Comparing 3 values
I am kind of following what your saying but not quite could you provide me with an example?
-
Feb 14th, 2007, 08:20 AM
#4
Thread Starter
Addicted Member
Re: Comparing 3 values
Coded that is so I gather an idea what what your working with.
-
Feb 14th, 2007, 10:30 AM
#5
Hyperactive Member
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."
-
Feb 14th, 2007, 10:56 AM
#6
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.
-
Feb 14th, 2007, 11:02 AM
#7
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:
Public Sub OrderValues(ByRef Values() As Integer)
Array.Sort(Values)
End Sub
this is code to show an example of that routine
VB Code:
Dim MyValues() As Integer = {70, 5, 13, 54}
For Each I As Integer In MyValues
MessageBox.Show(I.ToString)
Next
OrderValues(MyValues)
For Each I As Integer In MyValues
MessageBox.Show(I.ToString)
Next
-
Feb 14th, 2007, 11:02 AM
#8
Frenzied Member
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.
-
Feb 14th, 2007, 03:42 PM
#9
Hyperactive Member
Re: Comparing 3 values
 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
-
Feb 14th, 2007, 03:44 PM
#10
Re: Comparing 3 values
 Originally Posted by high6
kleinma he hasn't learned how to use arrays yet.
he has now, hasn't he??
-
Feb 14th, 2007, 05:42 PM
#11
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:
Dim Value1 As Integer = 1000
Dim Value2 As Integer = 100
Dim Value3 As Integer = 10
DoSort(Value1, Value2, Value3)
Dim Counter As Integer = 0 'counter just to see how many times it loops
Do Until Value1 < Value2 AndAlso Value2 < Value3
If Value1 > Value2 Then
'switch em
Dim Temp As Integer = Value2
Value2 = Value1
Value1 = Temp
Else
If Value2 > Value3 Then
'switch em
Dim Temp As Integer = Value3
Value3 = Value2
Value2 = Temp
End If
End If
Counter += 1
Loop
'From Lowest To Highest
MessageBox.Show(Value1.ToString & ", " & _
Value2.ToString & ", " & _
Value3.ToString & ", loop count: " & _
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|