Jump to content

Find second highest value in a Tab: proper way


member42
 Share

Go to solution Solved by jguinch,

Recommended Posts

Hello,

I've one question i've done some basic function to find the max / min value in an array like this for the findMax: ( same logic for find min but using _min function)

Func findPremier($tab, $kl)
   if _Max($tab[$kl],$tab[$kl+1],$tab[$kl+2],$tab[$kl+3]) = $tab[$kl] Then
      return $kl
   ElseIf _Max($tab[$kl],$tab[$kl+1],$tab[$kl+2],$tab[$kl+3]) = $tab[$kl+1] Then
      return $kl+1
   elseif _Max($tab[$kl],$tab[$kl+1],$tab[$kl+2],$tab[$kl+3]) = $tab[$kl+2] Then
      return $kl+2
   Else
      return $kl+3
   EndIf
EndFunc 

The array is not order and i can't be ordered( unless there is no other solution)

How can i find the second max value of those 4 value ?

For example how can i find "5" in this array: [12;1;3;5] ?

Thanks if anyone can help.

I know of course i could do this:( but prefer avoid an ordering)

Func findSecond($tab, $kl)
  Order($tab)
  return $tab[1];
EndFunc
Edited by member42
Link to comment
Share on other sites

Dim $Array[4] = [12, 1, 3, 5]

$Min = $Array[0]
$Max = $Array[0]
For $i = 2 To UBound($Array) - 1
    If $Array[$i] > $Max Then $Max = $Array[$i]
    If $Array[$i] < $Min Then $Min = $Array[$i]
Next

ConsoleWrite('Min = ' & $Min & @CR)
ConsoleWrite('Max = ' & $Max & @CR)

Edited by Yashied
Link to comment
Share on other sites

That's the function min/max same as _Min and _Max in the library. i Need the second max value in this case 5 maybe i need to do something like that:

FindMax(array)
If array[0] != findMax(array)
SecondMax=array[0]
else
SecondMax=array[1]

for i=0 to unbound(array)-1 step 1
   if array[i] > secondMax and array[i] != findMax(array)
       secondMax = array[i]
Edited by member42
Link to comment
Share on other sites

  • Developers

Something like this?:

#include <Array.au3>
Local  $aObjs  = 1
Dim $a[5]= [12,1,3,5]
_ArraySort($a,1)
; Show second entry in the array
ConsoleWrite('$a[1] = ' & $a[1] & @CRLF)

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Solution

?

Local $Array[4] = [12, 1, 3, 5]

Local $iMax
Local $iMax2

For $i = 0 To UBound($Array) - 1
    If $Array[$i] > $iMax Then
        $iMax2 = $iMax
        $iMax = $Array[$i]
    ElseIf $Array[$i] > $iMax2 Then
        $iMax2 = $Array[$i]
    EndIf
Next

ConsoleWrite("Second max value : " & $iMax2)
Link to comment
Share on other sites

Local $Array[10] = [12, 27, 20, 120, 111, 215, 54, 8, 77, 114]
Local $iMAX = $Array[0]
Local $iMIN = $Array[0]
Local $iMAX_Next = $Array[0]

For $i = 0 To UBound($Array) - 1
    If $Array[$i] < $iMIN Then $iMIN = $Array[$i]
    If $Array[$i] > $iMAX Then
        $iMAX_Next = $iMAX
        $iMAX = $Array[$i]
    ElseIf $Array[$i] < $iMAX And $Array[$i] > $iMAX_Next Then
        $iMAX_Next = $Array[$i]
    EndIf
Next

ConsoleWrite("Maximum value is : " & $iMAX & @CR)
ConsoleWrite("Minimum value is : " & $iMIN & @CR)
ConsoleWrite("Second maximum value is : " & $iMAX_Next & @CR)

Edited by johnmcloud
Link to comment
Share on other sites

  • Developers

Yeah but in this soltuioni must sort the array and i highly prefer to not sort the array ( because if i should also sort 4 others array =/ and other boring stuff =/ )

I've only 4 elements so it's even better to hard code all the solution than this

Don't understand the issue. Just put the logic in a UDF and return the found value so you can reuse the logic on other arrays.

something like:

#include <Array.au3>
Local  $aObjs  = 1
Dim $a[5]= [12,1,3,5]
Dim $b[5]= [1,21,13,5]
ConsoleWrite("2nd value for $a=" & Get2ndValue($a) & @CRLF)
ConsoleWrite("2nd value for $b=" & Get2ndValue($b) & @CRLF)

Func Get2ndValue($aI)
    Local $aIS = $aI
    _ArraySort($aIS,1)
    Return $aIS[1]
EndFunc

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks folks!

@Jos the problem is that i've few array linked at this one for example:

array1 = playerName['test',"test2','test3','test4']

array2= playerScore['10",'120","56","42"]

array3= playerTotalScore["1110","800","587","950"]

If i sort the array2 i also need to swap all the others link to this one. So i can't afford this ( but ofc it's possible but more work to do so)

Thanks again to everyone.

I've almost done ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...