Shop Learn
How to get the Maximum or Minimum value of an array? — Parallax Forums

How to get the Maximum or Minimum value of an array?

kevinspacekevinspace Posts: 56
edited 2011-12-24 17:41 in Propeller 1
Hello everyone~
Are there any one could suggest that how to get the Maximum or Minimum value of an array?

Just like it:
x[0] := 0
x[1] := 1
x[2] := 2
...
x[10] := 10

Than I want to get the Maximum or Minimum Value of x array, are there any OBJ or command just like Max(x) or Min(x)?

Are there any example or any suggestion?
Thanks a lot.

Comments

  • pedwardpedward Posts: 1,591
    edited 2011-12-24 02:26
    If you are talking about finding the largest or smallest number in an array, there is no builtin for that.

    You would have to do:
    pub max(arr, n)
    result := 0
    repeat x from 0 to n
      if arr[x] > result
        result := arr[x]
    
    pub min(arr, n)
    result := 0
    repeat x from 0 to n
      if arr[x] < result
        result := arr[x]
    
    
  • kevinspacekevinspace Posts: 56
    edited 2011-12-24 02:34
    pedward,
    I am grateful for your assistance~

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-12-24 04:49
    @pedward:
    The idea is ok, but the implementation won't work!
    arr is a local variable that can be found on the stack. You can't pass arrays, only 32-bit values or pointers.
    arr[x] does not work, as it treats a variable like an array. arr[0] would in fact give you the content of arr, but arr[1] would give you the content of n. (the 2nd parameter on the stack) From there on you operate in memory areas which can contain anything.

    What you have to do is pass the address of the array to the function, which means that accessing the elements has to be changed.

    Additionally there are operators in SPIN which return the min and the max values, so you don't need to use the if-statement.
    pub max(arr_ptr, n)
      result :=0
      repeat x from 0 to n
        result #>= long[ arr_ptr ][ x ] 
    

    You'd call max like that:
    VAR
      long myArray[10]
    
    PUB main | maxVal
      maxVal := max( @myArray, 10 )
    

    PS: By the way ... there is another problem (in both implementations) if the numbers are allowed to be negative or the numbers are unsigned because SPIN deals with signed numbers. The negative number problem can be fixed easily by initializing result with the lowest possible negative number. Unsigned numbers need to be treated differently.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 22,981
    edited 2011-12-24 09:01
    I think both of you meant repeat x from 0 to n - 1, right? Also, you can't name the method max, since that's a reserved word, and don't forget to declare x. You can save one iteration by dong this:
    pub maxval(arr_ptr, n) | i
      result := long[arr_ptr]
      repeat i from 1 to n - 1
        result #>= long[arr_ptr][i]
    

    -Phil
  • kwinnkwinn Posts: 8,691
    edited 2011-12-24 10:00
    If you store the first element of the array in result rather than 0 it will handle negative numbers automatically. Also, you can find max_result and min_result with just one pass through the loop.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-12-24 11:27
    Thanks for the pointers, that's why teamwork in most cases works better than single play ;o)

    Merry Xmas
  • kuronekokuroneko Posts: 3,623
    edited 2011-12-24 17:41
    pub maxval(arr_ptr, n) | i
      result := long[arr_ptr]
      repeat i from 1 to n - 1
        result #>= long[arr_ptr][i]
    
    long[base][idx] access is slow. This version - although slightly bigger - speeds things up a bit. Merry Christmas!
    pub maxval2(addr, n)
    
      addr := (addr - @addr) >> 2
      result := addr[addr++]
      repeat n - 1
        result #>= addr[addr++]
    
Sign In or Register to comment.