How to get the Maximum or Minimum value of an array?
kevinspace
Posts: 56
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.
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
You would have to do:
I am grateful for your assistance~
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.
You'd call max like that:
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
Merry Xmas