Obtain highest array value?

Hi,
If I have an array containing an unknown amount of integers, such as:
36, 212, 137, 96

Is it possible to obtain the highest value from the array’s elements (in this case, 212)?
If so, can someone please advise me how to do this.

I have looked at max, but I’m not sure if this is what I need to achieve this?

Example:

Dim maxValue As Integer = max(myArray())

Any help appreciated.
Thank you all in advance.

  dim values() as Integer = Array(36, 212, 137, 96)
  values.Sort
  
  dim first as Integer = values(0)
  dim last as Integer = values(values.Ubound)
  
  Break

Amando,
The array already exists, and I cannot hard-code the values, as I have no idea what values the array will contain.

Would my code below be correct:

myArray().Sort dim highestValue as Integer = myArray(myArray.Ubound)

You want a way to keep referenced this array on Integers.

A method like or computer property like:

Method Last( values as Integer() ) as Integer

values.Sort
 
Return(value ( value.ubound ) )

Untested

Should work. It depends on what you want to implement to the latests ( think in an OOP point of view )

Hope it helps!

Thanks Amando - I think sort was exactly what I was looking for (hopefully) :slight_smile:

Indeed! :wink:

The problem with Sort is that it will change the order of your array. It might also not be the fastest, although you’d have to test. This will work without changing the order.

dim highest as integer = arr( 0 )
for each element as integer in arr
  if element > highest then
    highest = element
  end if
next

return highest

Great to know Kem. I was not aware of sort issues…

Thanks Kem.
What does arr( 0 ) refer to?

Am I correct in presuming I need to replace arr( 0 ) with the name of my array - like this:

[code]Dim highest as integer = myArray(0)
for each element as integer in myArray()
if element > highest then
highest = element
end if
next

return highest[/code]

Right.

Thank you very much Kem - I appreciate your help.

Just out of curiosity, how do you guys compare execution times?
I have never understood how you do that?

Do I have to sit next to my computer with a stopwatch :slight_smile: :slight_smile:

Thanks.

Check Microseconds before and after each process and compare the differences.

Hmmm - will look into that too.
Thanks.

[code]Dim highest as integer
for each element as integer in myArray()
highest = max(highest, element)
next

return highest[/code]

(can’t resist)

The first pirate in the list…

[quote=194361:@Jeff Tullin](can’t resist)

The first pirate in the list…[/quote]

Early morning moan/chuckle. :slight_smile:

Michel, the only problem with your version is, what if the highest value is a negative number?

Also, unless it’s silently overloaded, max will convert the values to double which is why I always stayed away from it for integers. I should speed check that.

Max and your code would give the same result.

50 > -100

It would take working with abs() to take into account negative numbers as “higher” than positive ones.

That I did not know. Indeed it can probably slow things down.

highest is initialized to zero. If the entire array were comprised of negative numbers, the result would be incorrect. That’s why I initialized to the first element.