Shop OBEX P1 Docs P2 Docs Learn Events
Minimum Value in Array — Parallax Forums

Minimum Value in Array

XanfarasXanfaras Posts: 7
edited 2010-01-13 22:42 in BASIC Stamp
Hello again all,
I am trying to choose the minimum value from an array. Do I need to manually go through and test each element, or is there some sort of function I am not yet aware of?

Ex.
array = 10, 2, 5, 1, 7, 8

Ideally, I would actually like it to return 3 here (the minimum value of 1 is the element number 3), but if it would return 1 I could easily look that up. Otherwise I'm using a loop similar to that below.

min = 0
For i = 1 to 6
if array(i) < array(min) then min = i
next

Thanks guys,
Xan

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-13 16:51
    You need to go through the whole array and test each element. There is no built-in function that will do this for you. There are MAX and MIN operators, but these simply return the highest or lowest of two values (x MAX y or x MIN y)

    Post Edited (Mike Green) : 1/13/2010 4:56:08 PM GMT
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-01-13 22:42
    Not faster or better, but here is another way that uses MAX and LOOKDOWN.

    y=255
    FOR i=0 TO 5
      y = x(i) MAX y   ' finds minimum value y
    NEXT
    LOOKDOWN y, =[noparse][[/noparse]x(0),x(1),x(2),x(3),x(4),x(5)], i   ' finds index of minimum value
    debug CR, DEC y, TAB, DEC i
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.