Loading ...
Sorry, an error occurred while loading the content.

19835RE: [Clip] Finding Minimum Value in Array

Expand Messages
  • Pete Turner
    Nov 1, 2009
    • 0 Attachment
      G'day Flo,

      How about just looping through the array, looking for values that are successively lower than a value known to be greater than the highest vaue in the array?

      ^!Set %lowest%=999999999999
      ^!SetArray %array%=3;8;329;1;1.25;7;10.34;4;9;6;2;5;11
      ^!Set %index%=1
      ^!If ^%array^%index%% < ^%lowest% ^!Set %lowest%=^%array^%index%%
      ^!Inc %index%
      ^!If ^%index% <= ^%array0% SKIP_-2
      ^!Info ^%lowest%
      ; ^%lowest% = 1

      Or, if you want the highest value:-

      ^!Set %highest%=0 ; providing there are no negative values
      ^!SetArray %array%=3;8;329;1;1.25;7;10.34;4;9;6;2;5;11
      ^!Set %index%=1
      ^!If ^%array^%index%% > ^%highest% ^!Set %highest%=^%array^%index%%
      ^!Inc %index%
      ^!If ^%index% <= ^%array0% SKIP_-2
      ^!Info ^%highest%
      ; ^%highest% = 329

      It doesn;'t matter if there are duplicate values in the array.

      Pete


      > -----Original Message-----
      > From: ntb-clips@yahoogroups.com
      > [mailto:ntb-clips@yahoogroups.com] On Behalf Of flo.gehrke
      > Sent: Monday, 2 November 2009 15:19
      > To: ntb-clips@yahoogroups.com
      > Subject: [Clip] Finding Minimum Value in Array
      >
      >
      >
      > Hi all,
      >
      > What's the best way to find the minimum value in an array of numbers?
      >
      > For example, in: 3,8,329,1,1.25,7,10.34,4,9,6,2,5,11.
      >
      > To do it with commands like 'If A > B' etc seems to be a
      > complicated undertaking. Also the MIN or MAX operators to be
      > used with ^$Calc$ are not very useful. As far as I can see,
      > they compare two numbers only:
      >
      > ^!Info ^$Calc(MIN(3;6))$
      >
      > outputs '3' but 'MIN(3;6;9)' returns an "error in expression" message.
      >
      > I found a small utility called EVAL.EXE that you can download from
      >
      > http://tp.lc.ehu.es/jma/msdos.html
      > <http://tp.lc.ehu.es/jma/msdos.html>
      >
      > for free. It's from Juan M. Aguirregabiria, teaching
      > Theoretical Physics at Euskal Herriko Unibertsitatea, Bilbao.
      > I copied the EVAL.EXE and EVAL.HLP to C:\Windows, and it
      > seemes to work fine.
      >
      > For example...
      >
      > ^!Set %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Info ^$GetDosOutput(EVAL MIN(^%Array%))$
      >
      > correctly outputs '1'.
      >
      > The only problem I've seen so far: It seems to return a CRNL
      > with the output. So we get into trouble when using the result
      > in a command like ^!IfDiff. So...
      >
      > ^!Set %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Set %Min%=^$GetDosOutput(EVAL MIN(^%Array%))$
      > ^!IfDiff "^%Min%" "1" Next Else Skip_2
      > ^!Info Different!
      > ^!Goto End
      > ^!Info Not different!
      >
      > provides a wrong result. But when removing the CRNL, the
      > result will be correct:
      >
      > ^!Set %Array%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Set %Min%=^$GetDosOutput(EVAL MIN(^%Array%))$
      > ^!Set %Min%=^$StrReplace("^P";"";^%Min%;0;0)$
      > ^!IfDiff "^%Min%" "1" Next Else Skip_2
      > ^!Info Different!
      > ^!Goto End
      > ^!Info Not different!
      >
      > Please let me know if there are more problems with this
      > utility or if there is any better solution. Thanks!
      >
      > Flo
      >
      > P.S. I created a little "Sorting Machine" for a more
      > intensive testing of Eval:
      >
      > ^!SetListDelimiter ,
      > ^!SetArray %Dig%=3,8,329,1,1.25,7,10.34,4,9,6,2,5,11
      > ^!Set %Nr%=1; %From%=1; %Final%=^%Dig0%
      >
      > :Loop
      > ^!Set %Val%=^%Dig^%Nr%%
      > ^!Append %SubArray%=^%Val%,
      > ^!Inc %Nr%
      > ^!If ^%Nr% > ^%Final% Check
      > ^!Goto Loop
      >
      > :Check
      > ; Remove last comma in SubArray
      > ^!Set %SubArray%=^$StrDeleteRight(^%SubArray%;1)$
      > ; Use EVAL.EXE to find minimum value in SubArray
      > ^!Set %Min%=^$GetDosOutput(EVAL MIN(^%SubArray%))$
      > ; Remove CRNL produced by EVAL
      > ^!Set %Min%=^$StrReplace("^P";"";"^%Min%";0;0)$
      > ^!ClearVariable %SubArray%
      > ^!Set %ANr%=1
      >
      > :ANrLoop
      > ; Find number of minimum value in array
      > ^!IfDiff "^%Min%" "^%Dig^%ANr%%" Next Else Reorder
      > ^!Inc %ANr%
      > ^!Goto ANrLoop
      >
      > :Reorder
      > ^!Set %Park%=^%Dig^%ANr%%
      > ^!Set %Dig^%ANr%%=^%Dig^%From%%
      > ^!Set %Dig^%From%%=^%Park%
      > ^!Inc %From%
      > ^!If ^%From%=^%Final% End
      > ^!Set %Nr%=^%From%
      > ^!Goto Loop
      >
      > :End
      > ^!Info Sorted: ^%Dig%
      > ^!ClearVariable %SubArray%
      >
      > (Please note: No duplicate numbers allowed)
      >
      >
      >
      >
      >
    • Show all 15 messages in this topic