'This function gives max value of int array without sorting an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viratkothari
    New Member
    • Mar 2007
    • 1

    'This function gives max value of int array without sorting an array

    Code:
    Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer
    'This function gives max value of int array without sorting an array
    Dim i As Integer
    Dim MaxIntegersIndex As Integer
    MaxIntegersIndex = 0
    
    For i = 1 To UBound(TheArray)
        If TheArray(i) > TheArray(MaxIntegersIndex) Then
            MaxIntegersIndex = i
        End If
    Next
    'index of max value is MaxValOfIntArray
    MaxValOfIntArray = TheArray(MaxIntegersIndex)
    End Function
    Last edited by willakawill; Mar 5 '07, 06:52 AM. Reason: please use code tags when posting code
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Did you have a question for us?

    Comment

    • vijaydiwakar
      Contributor
      • Feb 2007
      • 579

      #3
      Originally posted by viratkothari
      Code:
      Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer
      'This function gives max value of int array without sorting an array
      Dim i As Integer
      Dim MaxIntegersIndex As Integer
      MaxIntegersIndex = 0
      
      For i = 1 To UBound(TheArray)
          If TheArray(i) > TheArray(MaxIntegersIndex) Then
              MaxIntegersIndex = i
          End If
      Next
      'index of max value is MaxValOfIntArray
      MaxValOfIntArray = TheArray(MaxIntegersIndex)
      End Function
      Is it question or answer?

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by viratkothari
        Code:
        Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer
        'This function gives max value of int array without sorting an array
        Dim i As Integer
        Dim MaxIntegersIndex As Integer
        MaxIntegersIndex = 0
        
        For i = 1 To UBound(TheArray)
            If TheArray(i) > TheArray(MaxIntegersIndex) Then
                MaxIntegersIndex = i
            End If
        Next
        'index of max value is MaxValOfIntArray
        MaxValOfIntArray = TheArray(MaxIntegersIndex)
        End Function

        Does u have the code simplified than this?

        see it may easy for 100 to 200 array index

        consider if i have 10,000 array index, do u think i have to execute 10000 times of that if condition and may be upto 9999 times of assigenment statement.?

        Comment

        Working...