Finding the biggest number out of 8 variables

M

Mojo

Hi All

Does anybody know of an easy way to find out which variable has the highest
value out of 8 variables?

For example, I have these values in 8 vars:

3 0 7 30 37 17 0 7

Although from a human's perspective it's easy to see that the 5th var has
the highest value, how can I chose the appriopriate var programmatically?

Thanks
 
R

Ralph

Mojo said:
Hi All

Does anybody know of an easy way to find out which variable has the highest
value out of 8 variables?

For example, I have these values in 8 vars:

3 0 7 30 37 17 0 7

Although from a human's perspective it's easy to see that the 5th var has
the highest value, how can I chose the appriopriate var programmatically?

[Would be easier to construct if the values were in an Array...]

Create a 9th variable. Call it "Highest".
Place the first value in "Highest".
Conditionally test each subsequent variable's value to see if it is higher
than "Highest".
If it is replace the value in "Highest" with the new value. If not go to the
next variable.
At the end of 8 tests what remains in "Highest" is the highest value.

-ralph
 
B

Bob Riemersma

Ralph said:
Create a 9th variable. Call it "Highest".
Place the first value in "Highest".
Conditionally test each subsequent variable's value to see if it is higher
than "Highest".
If it is replace the value in "Highest" with the new value. If not go to
the
next variable.
At the end of 8 tests what remains in "Highest" is the highest value.

-ralph

Similar idea:

Option Explicit

Private Function HighestOf(ParamArray Values())
Dim I As Integer

HighestOf = Values(0)
For I = 1 To UBound(Values)
If Values(I) > HighestOf Then HighestOf = Values(I)
Next
End Function

Private Sub Main()
Dim S As Single

S = 3.14159
MsgBox HighestOf(1, 3, 5, 7, 12, S)
End Sub
 
J

James Hahn

The problem with determining which variable has the highest value lies in
how you are going to nominate the variables involved. By far the simplest
solution is to use an array. That way, the array index can be used to
nominate which variable is the one of interest.

If it is acceptable to refer to the variables by an array index instead of
by name, then copying the values to an array, searching the array for the
highest value and returning that array index is relatively simple. If
necessary, you could then use a table lookup to convert the array index back
into a variable name.

For instance, if the values are copied to an array a() the following
function will return the index of the array for the highest value (or the
first of multiple equal high values).

Function Highest (A() as Double, Count as Integer)
Dim I as Integer, J as Integer, N as Double
I = 1
N = A(I)
For J = 2 To Count
If A(J) > N then
N = A(J)
I = J
End If
Next
Highest = I
End Function
 
G

Greg Maxey

Or you could load the variables into an array, sort the array, and then take
the last memeber:

Sub ScratchMacro()
Dim myArray(7)
Dim i As Long
Randomize
For i = 0 To UBound(myArray)
'Generate and load random value between 1 and 100
myArray(i) = Int((100 * Rnd) + 1)
Next i
WordBasic.SortArray myArray
MsgBox "Min = " & myArray(LBound(myArray))
MsgBox "Max = " & myArray(UBound(myArray))
End Sub
 
M

macropod

Answered in microsoft.public.excel.misc

Please don't post the same question in multiple forums without linking all of them. Your post in microsoft.public.excel.misc shows
that you know how to do this.

Plus, if your query is specific to one application, it's best limiting the post to a forum dealing with that application - the range
of newsgroups you've posted to suggest you haven't given much though to this.
 
J

James Hahn

OP is trying to find which variable contains the highest value, not what
that highest value is. Sorting the array is going to break the connection
between the array member and the variable it was initialized from. Of
course, it's possible to scan the variables to find which one matches the
last (highest) member of the sorted array, but that negates any advantage
that might have been obtained from sorting it in the first place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top