Determine which variable in a group contains the largest value! — Parallax Forums

# Determine which variable in a group contains the largest value!

Posts: 327

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

• Posts: 8,997
edited 2024-01-12 18:13

I'm coding as I'm having lunch, but this should work. Your values have to be an array of longs.

```pub find_max(p_vals) : idx | maxval, check, x

idx := -1
maxval := long[p_vals]

repeat x from 1 to 9
check := long[p_vals][x]
if (check > maxval)
maxval := check
idx := x
```

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.

• Posts: 6,558

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.

• Posts: 327

Sorry. I don't understand what "Pass a pointer to the array" means.
Aaron

• Posts: 8,997
edited 2024-01-12 18:02

"Pass a pointer" means to pass the address of object/variable. In Spin, the @ operator does that. Let's say you have this array:

```var

long  myValues[10]

```

In this case you would call that function with:

```  i := find_max(@myValues)
```
• Posts: 8,997
edited 2024-01-12 23:00

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:

```var

long  variable01
long  variable02
long  variable03
long  variable04
long  variable05
long  variable06
long  variable07
long  variable08
long  variable09
long  variable10

```

You can treat this group like an array with:

```  i := find_max(@variable01)
```

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...

```  theMaxVal := long[@variable01][i]
```

...where i was returned from the find_max() function.

• Posts: 327

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

• Posts: 327

Thanks Jon
Got it working great! Corrected a few of my mistakes and learned again from you.
Aaron

• Posts: 6,658
edited 2024-01-23 00:10

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

• Posts: 15,406

I don't understand what Kuroneko wrote at all. Shouldn't it just be this:

```pub maxval2(addr, n)

repeat n - 1
```
• Posts: 4,674

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.

• Posts: 15,406

```pub maxval2(addr, n)

```  max_val := register[0] #> register[1] #> register[2] #> register[3] #> register[4] #> register[5] #> register[6] #> register[7] #> register[8] #> register[9]