|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Finding the biggest number out of 8 variablesHi 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
Show quote
Hide quote
"Mojo" <please@dont.spam.com> wrote in message [Would be easier to construct if the values were in an Array...]news:%23i4vEOf4JHA.3860@TK2MSFTNGP05.phx.gbl... > 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? > 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 "Ralph" <nt_consultin***@yahoo.com> wrote in message Similar idea:news:%23q1$jmf4JHA.4632@TK2MSFTNGP02.phx.gbl... > 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 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 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 Bob Riemersma wrote: Show quoteHide quote > "Ralph" <nt_consultin***@yahoo.com> wrote in message > news:%23q1$jmf4JHA.4632@TK2MSFTNGP02.phx.gbl... >> 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 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. Show quoteHide quote "Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message news:O30mLvk4JHA.5728@TK2MSFTNGP03.phx.gbl... > 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 > > > Bob Riemersma wrote: >> "Ralph" <nt_consultin***@yahoo.com> wrote in message >> news:%23q1$jmf4JHA.4632@TK2MSFTNGP02.phx.gbl... >>> 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 > > -- > Greg Maxey - Word MVP > > My web site http://gregmaxey.mvps.org > Word MVP web site http://word.mvps.org > > > 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 Show quoteHide quote "Mojo" <please@dont.spam.com> wrote in message news:%23i4vEOf4JHA.3860@TK2MSFTNGP05.phx.gbl... > 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 > > > 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. -- Show quoteHide quoteCheers macropod [Microsoft MVP - Word] "Mojo" <please@dont.spam.com> wrote in message news:%23i4vEOf4JHA.3860@TK2MSFTNGP05.phx.gbl... > 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 > > > |
|||||||||||||||||||||||