Results 1 to 29 of 29

Thread: [RESOLVED] Loop through array to get min and max values

  1. #1

    Thread Starter
    Banned
    Join Date
    Apr 2014
    Location
    Ireland
    Posts
    3

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

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    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)))
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  6. #6
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Loop through array to get min and max values

    Quote Originally Posted by Shaggy Hiker View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Loop through array to get min and max values

    Quote Originally Posted by dday9 View Post
    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

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Loop through array to get min and max values

    Quote Originally Posted by kaliman79912 View Post
    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

  10. #10
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Loop through array to get min and max values

    Quote Originally Posted by Shaggy Hiker View Post
    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

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  13. #13
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Loop through array to get min and max values

    Quote Originally Posted by techgnome View Post
    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

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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.

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  17. #17

    Thread Starter
    Banned
    Join Date
    Apr 2014
    Location
    Ireland
    Posts
    3

    Re: Loop through array to get min and max values

    Joacim Andersson, I'll do as Im please. **** off

  18. #18
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Loop through array to get min and max values

    Quote Originally Posted by DylanM View Post
    Joacim Andersson, I'll do as Im please. **** off
    WOW! Let me guess. This is the only question you intend on asking here.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  19. #19
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541

    Re: Loop through array to get min and max values

    Quote Originally Posted by DylanM View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Loop through array to get min and max values

    No, he's still hung up on post #2, but still...

    WOW

  21. #21
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Loop through array to get min and max values

    Quote Originally Posted by DylanM View Post
    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"

  22. #22
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  23. #23
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [RESOLVED] Loop through array to get min and max values

    Can you give me tonight's Powerball numbers, too?!

  24. #24
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541

    Re: [RESOLVED] Loop through array to get min and max values

    Quote Originally Posted by kaliman79912 View Post
    I forsee the future... the list of members of the forum will decrease by one really soon
    Already done...


    Quote Originally Posted by vbfbryce View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  25. #25
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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!

  26. #26
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  27. #27
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: [RESOLVED] Loop through array to get min and max values

    Have to match at least 5 of the numbers, yes!

  28. #28
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  29. #29
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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
  •  



Click Here to Expand Forum to Full Width