Results 1 to 5 of 5

Thread: [RESOLVED] Compare 6 numbers

  1. #1

    Thread Starter
    Lively Member Arispan's Avatar
    Join Date
    Apr 2010
    Location
    Greece, Near Athens
    Posts
    98

    Resolved [RESOLVED] Compare 6 numbers

    Hello everyone,

    I just moved on from vb6 to vb.net so be easy with me.

    Here is the problem: I have 6 numbers in an array(dim ascore(5)) and I want to find who is the bigger one(I would like the number of the variable in the array to be returned, like ascore(2) or ascore(5)...). My mind is stuck! Can someone help me?
    MOBO: Asrock X58 Extreme
    CPU : Core i7 950 @ 3.07Ghz
    Ram : 8GB
    Hard Drive : 1.5 TB (1 TB, 500GB)
    Graphics : ATI Radeon HD 5670
    Dual Boot : Windows XP Home, Windows 7 Ultimate x64


    _____________________
    If a reply has helped you, please consider rating it.

  2. #2
    Member
    Join Date
    Sep 2010
    Posts
    38

    Re: Compare 6 numbers

    Hope this could help...
    Code:
       Dim ascore(5) As Integer
            Dim largest As New ArrayList
    
            largest.Add(0)
    
            'This code provide a sample values in the array
            ascore(0) = 2
            ascore(1) = 6
            ascore(2) = 1
            ascore(3) = 4
            ascore(4) = 6
            ascore(5) = 2
    
            For i As Integer = 1 To ascore.Length - 1
                If ascore(i) > ascore(largest(0)) Then
                    largest.Clear()
                    largest.Add(i)
                ElseIf ascore(i) = ascore(largest(0)) Then
                    largest.Add(i)
                End If
            Next
    
            Dim strLargest As String = "The value(s) in: " & Chr(13)
    
            For Each items As Integer In largest
                strLargest &= "ascore(" & items & ")" & Chr(13)
            Next
            strLargest &= "which is " & ascore(largest(0)) & " is the largest"
            MsgBox(strLargest)
    Please Rate me If I helped.

    Do not just copy paste codes, try to understand and learn from it.

  3. #3
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Compare 6 numbers

    Have a look at the SortedList class (see link below). It has a Max() method.

    I moved from VB6 to .NET as well and it took me a while to get the hang of treating everything as an object, rather than writing code for it.


    http://msdn.microsoft.com/en-us/libr...(v=VS.90).aspx
    This world is not my home. I'm just passing through.

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Compare 6 numbers

    If you are using VB2008 or higher you can use the IEnumerable.Max extension method. Since an array implements IEnumerable it also has the Max method. Furthermore, to get the index, you would use the IndexOf method:
    Code:
    Dim max = ascore.Max()
    Dim index = ascore.IndexOf(max)
    Having said this, it's always good to try it manually since this is an operation you might need in different situations as well. To find the maximum in an array you simply loop through it and remember the currently highest number. If you come across an even higher number, you remember that one:
    Code:
    Dim max = Integer.MinValue  ' we start with the lowest value possible
    For Each number As Integer in ascore
       If number > max Then max = number
    Next

  5. #5

    Thread Starter
    Lively Member Arispan's Avatar
    Join Date
    Apr 2010
    Location
    Greece, Near Athens
    Posts
    98

    Re: Compare 6 numbers

    Thanks Nick!
    MOBO: Asrock X58 Extreme
    CPU : Core i7 950 @ 3.07Ghz
    Ram : 8GB
    Hard Drive : 1.5 TB (1 TB, 500GB)
    Graphics : ATI Radeon HD 5670
    Dual Boot : Windows XP Home, Windows 7 Ultimate x64


    _____________________
    If a reply has helped you, please consider rating it.

Tags for this Thread

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