Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Mar 2007
Posts: 1
#1: Mar 5 '07
Expand|Select|Wrap|Line Numbers
  1. Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer
  2. 'This function gives max value of int array without sorting an array
  3. Dim i As Integer
  4. Dim MaxIntegersIndex As Integer
  5. MaxIntegersIndex = 0
  6.  
  7. For i = 1 To UBound(TheArray)
  8.     If TheArray(i) > TheArray(MaxIntegersIndex) Then
  9.         MaxIntegersIndex = i
  10.     End If
  11. Next
  12. 'index of max value is MaxValOfIntArray
  13. MaxValOfIntArray = TheArray(MaxIntegersIndex)
  14. End Function

Lives Here
 
Join Date: Oct 2006
Posts: 1,626
#2: Mar 5 '07

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


Did you have a question for us?
vijaydiwakar's Avatar
Site Addict
 
Join Date: Feb 2007
Posts: 579
#3: Mar 5 '07

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


Quote:

Originally Posted by viratkothari

Expand|Select|Wrap|Line Numbers
  1. Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer
  2. 'This function gives max value of int array without sorting an array
  3. Dim i As Integer
  4. Dim MaxIntegersIndex As Integer
  5. MaxIntegersIndex = 0
  6.  
  7. For i = 1 To UBound(TheArray)
  8.     If TheArray(i) > TheArray(MaxIntegersIndex) Then
  9.         MaxIntegersIndex = i
  10.     End If
  11. Next
  12. 'index of max value is MaxValOfIntArray
  13. MaxValOfIntArray = TheArray(MaxIntegersIndex)
  14. End Function

Is it question or answer?
hariharanmca's Avatar
Lives Here
 
Join Date: Dec 2006
Location: Banglore/India
Posts: 1,987
#4: Mar 5 '07

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


Quote:

Originally Posted by viratkothari

Expand|Select|Wrap|Line Numbers
  1. Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer
  2. 'This function gives max value of int array without sorting an array
  3. Dim i As Integer
  4. Dim MaxIntegersIndex As Integer
  5. MaxIntegersIndex = 0
  6.  
  7. For i = 1 To UBound(TheArray)
  8.     If TheArray(i) > TheArray(MaxIntegersIndex) Then
  9.         MaxIntegersIndex = i
  10.     End If
  11. Next
  12. 'index of max value is MaxValOfIntArray
  13. MaxValOfIntArray = TheArray(MaxIntegersIndex)
  14. 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.?
Reply