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
Maximum value in an array
-
- Posts: 195
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
- BruceSteers
- Posts: 1565
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Maximum value in an array
I should think just itterate through it..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
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
BruceS
-
- Posts: 195
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Maximum value in an array
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.
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]
- BruceSteers
- Posts: 1565
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Maximum value in an array
You said you wanted to display max or min values!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]
If min is always zero then why do you say you want to find min value?"What is the best way to determine the max or min value in array?"
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
BruceS
-
- Posts: 195
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Maximum value in an array
Of course! I spotted my mistake earlier and found that this works:-
But your solution using MinVal = MyArrayOfNumbers[0] works fine and as you say, iteration is quicker than .Copy.
Thanks for your help
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
- BruceSteers
- Posts: 1565
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Maximum value in an array
WOW, i used the profiler and found out your way is actually faster, by quite a lot! LOLbill-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
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
BruceS
-
- Posts: 195
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Maximum value in an array
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.
Anyway thanks again.