-
Apr 14th, 2014, 12:59 PM
#1
Thread Starter
Banned
[RESOLVED] Loop through array to get min and max values
Hi. I need to loop through different parts of an array to get Max and Min values. Any help greatly appreciated.
-
Apr 15th, 2014, 09:30 AM
#2
Re: Loop through array to get min and max values
Post moved to its own thread. Do not hijack someone else's thread to ask your own question and never be rude to someone just because they pointed this out to you. Your rude post has been deleted, keep the language civil in the future.
-
Apr 15th, 2014, 09:46 AM
#3
Re: Loop through array to get min and max values
For the entire array, you could use .Max or .Min. Since you only want to search through a portion of the array you'd have to go about it a bit differently. Something like this should do:
Code:
Dim maxV as Integer = Integer.MinValue
Dim minV as Integer = Integer.MaxValue
For x = lowIndex to highIndex
If yourArray(x) > maxV Then
maxV = yourArray(x)
End If
If yourArray(x) < minV Then
minV = yourArray(x)
End If
Next
There are lots of assumptions in that code, such as assuming that the array contains integers. If that assumption is wrong, then the types would have to be changed to the correct type. This also retains the maximum and minimum values between lowIndex and highIndex, but it doesn't retain which index had the low and high values.
My usual boring signature: Nothing
-
Apr 15th, 2014, 09:55 AM
#4
Re: Loop through array to get min and max values
I would personally do this:
Code:
Dim intArray() As Integer = {56, 298, -14, 20}
Array.Sort(intArray)
Console.WriteLine(String.Format("Min: {0} Max: {1}", intArray(0), intArray(intArray.Length - 1)))
-
Apr 15th, 2014, 09:57 AM
#5
Re: Loop through array to get min and max values
That works if the search is for the min/max of the entire array, and you don't need to retain the original order of items in the array. Lord only knows what the OP wants.
My usual boring signature: Nothing
-
Apr 15th, 2014, 09:58 AM
#6
Re: Loop through array to get min and max values
Expanding on the solution proposed by Shaggy
Code:
Dim maxV As Integer = Integer.MinValue
Dim minV As Integer = Integer.MaxValue
For x As Integer = lowIndex To highIndex
maxV = Math.Max(maxV, yourArray(x))
minV = Math.Min(minV, yourArray(x))
Next
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 15th, 2014, 09:59 AM
#7
Re: Loop through array to get min and max values
Originally Posted by Shaggy Hiker
That works if the search is for the min/max of the entire array, and you don't need to retain the original order of items in the array. Lord only knows what the OP wants.
That's true. It's all very vague at this point.
-
Apr 15th, 2014, 10:03 AM
#8
Re: Loop through array to get min and max values
Originally Posted by dday9
I would personally do this:
Code:
Dim intArray() As Integer = {56, 298, -14, 20}
Array.Sort(intArray)
Console.WriteLine(String.Format("Min: {0} Max: {1}", intArray(0), intArray(intArray.Length - 1)))
Yep, that would work. I don't know if the performace is great if you need to sort the array. And would that be acceptable? I mean, to modifiy the order of the elements? Maybe, if the entire array is to be checked, as Shaggy said, you cold just
Code:
maxV = yourArray.Max
minV = yourArray.Min
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 15th, 2014, 10:19 AM
#9
Re: Loop through array to get min and max values
Originally Posted by kaliman79912
Expanding on the solution proposed by Shaggy
Code:
Dim maxV As Integer = Integer.MinValue
Dim minV As Integer = Integer.MaxValue
For x As Integer = lowIndex To highIndex
maxV = Math.Max(maxV, yourArray(x))
minV = Math.Min(minV, yourArray(x))
Next
When I saw that approach, I felt that I had to compare my code to that code. This code is certainly cleaner than what I had written, so I was curious as to whether or not it performed better or worse. Therefore, I put both sets of code into my test app and compared the performance.
What I wrote ends up being about 17% faster, which is a pretty trivial amount. It was consistent, though. Once again it shows that VB gives you many ways to solve a problem, some are verbose and others are concise. However, verbose always seems to top concise. I don't see why that should be.
My usual boring signature: Nothing
-
Apr 15th, 2014, 10:23 AM
#10
Re: Loop through array to get min and max values
Originally Posted by Shaggy Hiker
When I saw that approach, I felt that I had to compare my code to that code. This code is certainly cleaner than what I had written, so I was curious as to whether or not it performed better or worse. Therefore, I put both sets of code into my test app and compared the performance.
What I wrote ends up being about 17% faster, which is a pretty trivial amount. It was consistent, though. Once again it shows that VB gives you many ways to solve a problem, some are verbose and others are concise. However, verbose always seems to top concise. I don't see why that should be.
I have thought about that issue a lot. And I came to the conclusion that when the application is not performance critical, then using easier to read code is best; but when seconds or even milliseconds are important, then you go for the most efficient code. It is a mater of choice of course.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 15th, 2014, 10:32 AM
#11
Re: Loop through array to get min and max values
And just because we're throwing things around... here's yet another way:
Code:
Dim intArr As Integer() = {1, 2, 3, 6, 32, 16, 8, 9, 45, 335, 7, 878, 4, 566}
Dim startIdx As Integer = 5
Dim endIdx As Integer = 10
Dim intMax As Integer = intArr.Take(endIdx).Reverse.Take(startIdx).Reverse.Max
Dim intMin As Integer = intArr.Take(endIdx).Reverse.Take(startIdx).Reverse.Min
MessageBox.Show(String.Format("Max: {0} -- Min: {1}", intMax, intMin))
-tg
-
Apr 15th, 2014, 10:41 AM
#12
Re: Loop through array to get min and max values
Woof. You couldn't throw that far enough for my taste. It's certainly concise, but readable....not so much.
My usual boring signature: Nothing
-
Apr 15th, 2014, 10:41 AM
#13
Re: Loop through array to get min and max values
Originally Posted by techgnome
And just because we're throwing things around... here's yet another way:
Code:
Dim intArr As Integer() = {1, 2, 3, 6, 32, 16, 8, 9, 45, 335, 7, 878, 4, 566}
Dim startIdx As Integer = 5
Dim endIdx As Integer = 10
Dim intMax As Integer = intArr.Take(endIdx).Reverse.Take(startIdx).Reverse.Max
Dim intMin As Integer = intArr.Take(endIdx).Reverse.Take(startIdx).Reverse.Min
MessageBox.Show(String.Format("Max: {0} -- Min: {1}", intMax, intMin))
-tg
Very interesting, can you explain how this one works?
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 15th, 2014, 11:01 AM
#14
Re: Loop through array to get min and max values
The truth is, it doesn't work.
Using the indicies 5 and 10 make it seem to work, but that is only for that case.
This is the corrected version with different values that would show the original failing.
Code:
Dim intArr As Integer() = {1, 2, 3, 6, 32, 16, 8, 9, 45, 335, 7, 878, 4, 566}
Dim startIdx As Integer = 2
Dim endIdx As Integer = 8
Dim intMax As Integer = intArr.Take(endIdx).Reverse.Take(endIdx - startIdx).Reverse.Max
Dim intMin As Integer = intArr.Take(endIdx).Reverse.Take(endIdx - startIdx).Reverse.Min
MessageBox.Show(String.Format("Max: {0} -- Min: {1}", intMax, intMin))
If you put your cursor on Take, and hit F1, and after reading what Take does, then do the same on Reverse, and you should be able to figure out what it is doing. That might be more entertaining than just explaining it.
Also, if you wanted to include the endIdx value in the test, then you would have to increment the endIdx value by 1 before using the code as written (as take would return 8 values, not including the 8th index).
Last edited by passel; Apr 15th, 2014 at 11:13 AM.
-
Apr 15th, 2014, 12:02 PM
#15
Re: Loop through array to get min and max values
Bleh... yeah, I see why it doesn't work ... I just happened to hit the right numbers with 5/10 where it did work... even still... not that hard to fix - as you noted in the code... :P
-tg
-
Apr 16th, 2014, 04:23 AM
#16
Re: Loop through array to get min and max values
Reversing the array 4 times can be very slow if the array is large. If you want to go with this it's better to do it this way:
Code:
Dim intArr As Integer() = {1, 2, 3, 6, 32, 16, 8, 9, 45, 335, 7, 878, 4, 566}
Dim startIdx As Integer = 2
Dim endIdx As Integer = 8
Dim subsetArr As IEnumerable(Of Integer) = intArr.Take(endIdx + 1).Reverse.Take(endIdx - startIdx + 1).Reverse
Dim intMin As Integer = subsetArr.Min
Dim intMax As Integer = subsetArr.Max
MessageBox.Show(String.Format("Max: {0} -- Min: {1}", intMax, intMin))
Also note that I changed the code so that it actually works. You should Take(endIdx + 1) and Take(endIdx - startIdx + 1) since 0 is the first index in the array.
Last edited by Joacim Andersson; Apr 16th, 2014 at 04:31 AM.
-
Apr 16th, 2014, 07:34 AM
#17
Thread Starter
Banned
Re: Loop through array to get min and max values
Joacim Andersson, I'll do as Im please. **** off
-
Apr 16th, 2014, 07:39 AM
#18
Re: Loop through array to get min and max values
Originally Posted by DylanM
Joacim Andersson, I'll do as Im please. **** off
WOW! Let me guess. This is the only question you intend on asking here.
-
Apr 16th, 2014, 07:55 AM
#19
Re: Loop through array to get min and max values
Originally Posted by DylanM
Joacim Andersson, I'll do as Im please. **** off
Wow... I think JA's reply was more directed at me... so for you to go off on a mod like that... just wow...
-tg
-
Apr 16th, 2014, 08:02 AM
#20
Re: Loop through array to get min and max values
No, he's still hung up on post #2, but still...
WOW
-
Apr 16th, 2014, 08:18 AM
#21
Re: Loop through array to get min and max values
Originally Posted by DylanM
Joacim Andersson, I'll do as Im please. **** off
I'm sorry but in this forum you can't do as you please. We do have rules that you agreed to when you signed up. Since I already warned you about using foul language you may now go and do what you want somewhere else. Bye.
Last edited by Joacim Andersson; Apr 16th, 2014 at 06:06 PM.
Reason: Changed the typo "on" to "to"
-
Apr 16th, 2014, 08:41 AM
#22
Re: Loop through array to get min and max values
I forsee the future... the list of members of the forum will decrease by one really soon
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 16th, 2014, 08:48 AM
#23
Re: [RESOLVED] Loop through array to get min and max values
Can you give me tonight's Powerball numbers, too?!
-
Apr 16th, 2014, 08:53 AM
#24
Re: [RESOLVED] Loop through array to get min and max values
Originally Posted by kaliman79912
I forsee the future... the list of members of the forum will decrease by one really soon
Already done...
Originally Posted by vbfbryce
Can you give me tonight's Powerball numbers, too?!
1,2,3,4,5,6,7,8,9,10,11,12,13,14,.... oh... you meant tonight WINNING Powerball numbers...
-tg
-
Apr 16th, 2014, 08:54 AM
#25
Re: [RESOLVED] Loop through array to get min and max values
Yeah, the WINNING numbers!
I'll give you 1% of my winnings if you do!
-
Apr 16th, 2014, 09:50 AM
#26
Re: [RESOLVED] Loop through array to get min and max values
I love how this thread became a CC thread after the OP's second post. By the way I'll talk that 1% but do they have to be winning numbers?
-
Apr 16th, 2014, 10:36 AM
#27
Re: [RESOLVED] Loop through array to get min and max values
Have to match at least 5 of the numbers, yes!
-
Apr 16th, 2014, 10:40 AM
#28
Re: [RESOLVED] Loop through array to get min and max values
Let's just let this one die. It should have been locked anyways. I gave a serious answer because, while I felt the OP was a bit nutty, I thought they were fumbling towards reasonability. I'm sorry to see that was not the case and that the OP ended up just being pushy and uncivil.
Let's just let it fade away.
My usual boring signature: Nothing
-
Apr 16th, 2014, 06:02 PM
#29
Re: [RESOLVED] Loop through array to get min and max values
Well I actually thought that by just marking this thread as resolved (since the OP actually got his question answered before he insulted me) would be enough to end the discussion but since it apparently hasn't (and since this is not a CC thread) I guess I have to close it. (However this is not the CC forum and I shouldn't have to stop the discussion by closing the thread, you guys should know better!)
Last edited by Joacim Andersson; Apr 16th, 2014 at 06:07 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|