Determine which variable in a group contains the largest value!
AGCB
Posts: 327
in Propeller 1
In a group of 10 variables that are incrementing randomly or some not at all, after a short time (say 1 minute), I want to determine which of the variables contains the highest value.
Aaron
Comments
I'm coding as I'm having lunch, but this should work. Your values have to be an array of longs.
Pass a pointer to the array and you'll get back the index of the highest value -- or -1 if all values are the same.
Edit: Changed the starting index of the repeat loop to 1 as there is no need to compare the first variable with itself.
I see this and I think there should be a clever arbitration method of just looking at the Most Significant Bits to determine the largest value. Thinking of something like CANBUS or 1-Wire arbitration methods. ... I miss the old "Golf Challenges" I used to post.
Sorry. I don't understand what "Pass a pointer to the array" means.
Aaron
"Pass a pointer" means to pass the address of object/variable. In Spin, the @ operator does that. Let's say you have this array:
In this case you would call that function with:
Now... down the road you will learn that Spin treats RAM like a giant array, and variables of the same type will be placed into RAM in the order they're declared. What that means is that you could have this:
You can treat this group like an array with:
The code will iterate through variable01 through variable10 because they will be contiguous in RAM based on the position of their declaration.
If you want to know what the maximum value found was...
...where i was returned from the find_max() function.
I've been getting close even before your reply. I've been reading the Propeller manual for 3 hours. Finding a lot things I knew before but forgot by not using them!
Aaron
Thanks Jon
Got it working great! Corrected a few of my mistakes and learned again from you.
Aaron
This comment from Kuroneko added another brain-teaser to speed up the spin, thread "How to get the Maximum or Minimum value of an array?"
https://forums.parallax.com/discussion/comment/1061613/#Comment_1061613
I don't understand what Kuroneko wrote at all. Shouldn't it just be this:
No, because that only increments the address by 1 each time. The whacky version indexes into the long-sized local variables, so the increment by 1 increments the effective address by 4. Don't worry, I had to do a double take on that, too.
Oh, ha. I still can't read how his code works. How about this:
As an alternative to the original question: