Maximum value in an array

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Maximum value in an array

Post by bill-lancaster »

What is the best way to determine the max or min value in array?
At the moment I'm copying the array to a temporary array, sorting it then using array.max
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Maximum value in an array

Post by BruceSteers »

bill-lancaster wrote: Monday 11th July 2022 8:26am What is the best way to determine the max or min value in array?
At the moment I'm copying the array to a temporary array, sorting it then using array.max
I should think just itterate through it..
Dim MinVal, MaxVal As Integer

For Each CurVal As Integer in MyArrayOfNumbers
  MaxVal=Max(CurVal, MaxVal)
  MinVal=Min(CurVal, MinVal)
Next
If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Maximum value in an array

Post by bill-lancaster »

Thanks Bruce,
That's a nice idea but MinVal is always zero!
Also, copying the array of values into a temporary array then sorting it strangely sorts the original array.

Code: Select all

Dim MyArrayOfNumbers As Integer[] = [99, 91, 93, 92, 95, 90, 95, 100, 90, 90, 99]
Dim fTemp As Integer[]
fTemp = MyArrayOfNumbers
fTemp.Sort
Print fTemp[0];; MyArrayOfNumbers[0]
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Maximum value in an array

Post by BruceSteers »

bill-lancaster wrote: Tuesday 12th July 2022 9:06am Thanks Bruce,
That's a nice idea but MinVal is always zero!
Also, copying the array of values into a temporary array then sorting it strangely sorts the original array.

Code: Select all

Dim MyArrayOfNumbers As Integer[] = [99, 91, 93, 92, 95, 90, 95, 100, 90, 90, 99]
Dim fTemp As Integer[]
fTemp = MyArrayOfNumbers
fTemp.Sort
Print fTemp[0];; MyArrayOfNumbers[0]
You said you wanted to display max or min values!
"What is the best way to determine the max or min value in array?"
If min is always zero then why do you say you want to find min value?

Ahh no sorry i see what you mean now.
Try this...
 Dim MinVal As Integer = MyArrayOfNumbers[0]
 Dim MaxVal As Integer
 
For Each CurVal As Integer in MyArrayOfNumbers
  MaxVal=Max(CurVal, MaxVal)
  MinVal=Min(CurVal, MinVal)
Next

that will give MinVal an initial value that's not zero to begin with.

iterating through the array will be a much faster operation than making a copy then using sort on it.

PS. Sorting original array is not so strange as you are not copying the array you are making fTemp a pointer to the array.
fTemp = MyArrayOfNumbers.Copy() will make a copy.
If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Maximum value in an array

Post by bill-lancaster »

Of course! I spotted my mistake earlier and found that this works:-

Code: Select all

fTemp = MyArrayOfNumbers.Copy(0, MyArrayOfNumbers.Max)
fTemp.Sort()
Print fTemp[0];; fTemp[fTemp.Max]


But your solution using MinVal = MyArrayOfNumbers[0] works fine and as you say, iteration is quicker than .Copy.
Thanks for your help
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Maximum value in an array

Post by BruceSteers »

bill-lancaster wrote: Tuesday 12th July 2022 10:10am Of course! I spotted my mistake earlier and found that this works:-

Code: Select all

fTemp = MyArrayOfNumbers.Copy(0, MyArrayOfNumbers.Max)
fTemp.Sort()
Print fTemp[0];; fTemp[fTemp.Max]


But your solution using MinVal = MyArrayOfNumbers[0] works fine and as you say, iteration is quicker than .Copy.
Thanks for your help
WOW, i used the profiler and found out your way is actually faster, by quite a lot! LOL :)

I made this command...
Public Sub MinMaxValue((Array) As Variant[]) As Variant[]

  Dim a As Variant[] = Array.Sort()
  Return [a.First, a.Last]

End
it returns [MinValue, MaxValue]

So can be used like
Dim MinMax As Integer[] = MinMaxValue(MyArrayOfNumbers)
Print "Min="; MinMax[0];; "Max="; MinMax[1]

or
Print "Max="; MinMaxValue(MyArrayOfNumbers)[1]

and is speedy.
you need less help than you think lol :)
Sorry for my misunderstanding.
If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Maximum value in an array

Post by bill-lancaster »

No problem Bruce, it was a useful discussion I'm just surprised its not a buit-in function e.g. MyArray.MaxValue.
Anyway thanks again.
Post Reply