Jump to content

Extended _Max & _Min to cope with up to 20 numbers each


Recommended Posts

I have taken Jeremy Landes' _Max & _Min and improved them to compare up to 20 numbers

Test code

Local $nmax =_Max2(5,5,7,9,3,17,-1,3)
    ConsoleWrite('@@ Debug(1) : $nmax = ' & $nmax & @LF);### Debug Console
    Local $nmin =_Min2(5,5,7,9,3,17,-1,3)
    ConsoleWrite('@@ Debug(1) : $nmax = ' & $nmin & @LF);### Debug Console

And the code itself...

; _Max2                     Extends _Max to work with up to 20 numbers
Func _Max2($n01,$n02,$n03='',$n04='',$n05='',$n06='',$n07='',$n08='',$n09='',$n012='',$n013='',$n014='',$n015='',$n016='',$n017='',$n018='',$n019='',$n020='')
    If (Not IsNumber($n01)) Then
        SetError(1)
        Return (0)
    EndIf
    If (Not IsNumber($n02)) Then
        SetError(2)
        Return (0)
    EndIf
;
    Local $Res=$n01
    Local $idx
    Local $Val
    Local $Num
    Local $Nam
;
    For $idx=2 To 20
        If $idx<9 Then 
            $Nam='n0'&$idx
        Else
            $Nam='n'&$idx
        EndIf
        $Num=Eval($Nam)
        If IsNumber($Num) Then
            If $Num>$Res Then $Res=$Num
        Else
            ExitLoop
        EndIf
    Next
    Return $Res
EndFunc ;==>_Max2
; _Min2                     Extends _Min to work with up to 20 numbers
Func _Min2($n01,$n02,$n03='',$n04='',$n05='',$n06='',$n07='',$n08='',$n09='',$n012='',$n013='',$n014='',$n015='',$n016='',$n017='',$n018='',$n019='',$n020='')
    If (Not IsNumber($n01)) Then
        SetError(1)
        Return (0)
    EndIf
    If (Not IsNumber($n02)) Then
        SetError(2)
        Return (0)
    EndIf
;
    Local $Res=$n01
    Local $idx
    Local $Val
    Local $Num
    Local $Nam
;
    For $idx=2 To 20
        If $idx<9 Then 
            $Nam='n0'&$idx
        Else
            $Nam='n'&$idx
        EndIf
        $Num=Eval($Nam)
        If IsNumber($Num) Then
            If $Num<$Res Then $Res=$Num
        Else
            ExitLoop
        EndIf
    Next
    Return $Res
EndFunc ;==>_Min2
Edited by andyswarbs
Link to post
Share on other sites

Thought you might find this usefull also

returns the Min or Max number from 1 or 2 arrays

Test

Local $Array1[5] = [3, 2, 8, 5, 10]
Local $Array2[5] = ["3", "02", "8", "1", "20"]
Local $Array3[5] = ["3", "02", "8", "-1", "11"]
Local $Max, $Min

$Max = _MinMaxa($Array1, 1)
If Not @error Then
   ConsoleWrite('@@ Debug(1) :$Max = ' & $Max & @LF)
Else
   ConsoleWrite('@@ Debug(1) :Error = ' & @error & @LF)
EndIf

$Max = _MinMaxa($Array2, 1)
If Not @error Then
   ConsoleWrite('@@ Debug(1) :$Max = ' & $Max & @LF)
Else
   ConsoleWrite('@@ Debug(1) :Error = ' & @error & @LF)
EndIf

$Max = _MinMaxa($Array1, 1, $Array3)
If Not @error Then
   ConsoleWrite('@@ Debug(1) :$Max = ' & $Max & @LF)
Else
   ConsoleWrite('@@ Debug(1) :Error = ' & @error & @LF)
EndIf

$Min = _MinMaxa($Array1)
If Not @error Then
   ConsoleWrite('@@ Debug(1) :$Min = ' & $Min & @LF)
Else
   ConsoleWrite('@@ Debug(1) :Error = ' & @error & @LF)
EndIf

$Min = _MinMaxa($Array2)
If Not @error Then
   ConsoleWrite('@@ Debug(1) :$Min = ' & $Min & @LF)
Else
   ConsoleWrite('@@ Debug(1) :Error = ' & @error & @LF)
EndIf

$Min = _MinMaxa($Array1, 0, $Array3)
If Not @error Then
   ConsoleWrite('@@ Debug(1) :$Min = ' & $Min & @LF)
Else
   ConsoleWrite('@@ Debug(1) :Error = ' & @error & @LF)
EndIf

#include <Array.au3>
; return min or max number from 1 or 2 arrays
Func _MinMaxa($a_nums01, $MaxFlag = 0, $a_nums02 = 0)
   If Not IsArray($a_nums01) Then
      SetError(1)
      Return (0)
   EndIf
   If @NumParams = 3 Then
      If Not IsArray($a_nums02) Then
         SetError(3)
         Return (0)
      EndIf
   EndIf
  ;
   Local $idx
  ;
   For $idx = 0 To UBound($a_nums01) - 1
      If StringIsFloat($a_nums01[$idx]) Or StringIsInt($a_nums01[$idx]) Then
         $a_nums01[$idx] = Number($a_nums01[$idx])
      Else
         SetError(2)
         Return (0)
      EndIf
   Next
   _ArraySort($a_nums01, $MaxFlag)
   If @NumParams = 3 Then
      For $idx = 0 To UBound($a_nums02) - 1
         If StringIsFloat($a_nums02[$idx]) Or StringIsInt($a_nums02[$idx]) Then
            $a_nums02[$idx] = Number($a_nums02[$idx])
         Else
            SetError(4)
            Return (0)
         EndIf
      Next
      _ArraySort($a_nums02, $MaxFlag)
      If $MaxFlag Then
         If Number($a_nums01[0]) > Number($a_nums02[0]) Then
            Return $a_nums01[0]
         Else
            Return $a_nums02[0]
         EndIf
      Else
         If Number($a_nums01[0]) < Number($a_nums02[0]) Then
            Return $a_nums01[0]
         Else
            Return $a_nums02[0]
         EndIf
      EndIf
   EndIf
   Return $a_nums01[0]
EndFunc  ;==>_MinMaxa

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...