How to select highest array value?
lardom
Posts: 1,659
In Spin I was wondering how to have a wheeled robot rotate 180° taking distance measurements every 30°. The variables would be named "270°, 300°..etc. I thought I could use a case statement but it does not seem to apply. Is there a way to select a variable based on one that is 'closest' to a target number? I'm only interested in the highest or the lowest value not the average. (If an array would add to the overhead I wouldn't use one.)
Comments
But if you have an array of many values, you could use one of the sorting algorithms here: http://obex.parallax.com/objects/715/ . Then the first index would be the lowest, and the last would be the highest.
Or you make a "lowest" and "highest" variable and check to see if the newest input it lower or higher (respectively) than the existing value and store the new value.
There are a lot of pieces to this puzzle. The idea was to have the robot rotate 360° taking a distance measurement every 30° then using case to turn the robot toward the greatest distance. An interesting puzzle that I'm still trying to solve.
"return MyVar" will replace what I normally write; "result := MyVar". What does this expression mean? "long[p_array]" Is it the long aligned data or the memory address? I ask because you use "+= 4" which points to the next long address.