Array - Finding position of largest value

# Thread: Array - Finding position of largest value

1. booth Guest

## Array - Finding position of largest value

Can anyone tell me how to find the position of the largest value of an array?<BR><BR>For example, how would I find out that the position of the largest value of the following array is 3?<BR>(1,0,3,7,4,3,2)<BR><BR>Thanks.

2. Steve Cimino Guest

## Manually

No built in function to do that... should be rather easy though.<BR><BR>x = myarray(0)<BR>myelement = 0<BR><BR>For i = 1 to UBound(myArray)<BR> If x &#062; myarray(i) Then<BR> x = myarray(i)<BR> myelement = i<BR> End If<BR>Next

3. booth Guest

## RE: Manually

Thanks for the reply. Does x need to be incremented after the End If statement?

4. booth Guest

## RE: Manually

Also, how would I determine if there is a tie for the largest number? Thanks.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

Q: Does x need to be incremented after the loop.<BR>A: The only purpose of x is to remember the value of the largest element. Why would you want to have a value one larger than the largest value found???<BR><BR>myelement is the variable that remembers the *position* of the largest element.<BR><BR>*************<BR><BR>Q: how would I determine if there is a tie for the largest number? <BR><BR>A: Change the code:<BR><BR>&#060;%<BR>maxValue = myarray(0) <BR>maxElement = 0 <BR>maxCount = 0<BR><BR>For i = 1 to UBound(myArray) <BR>&nbsp; &nbsp; If maxValue &#060; myarray(i) Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; maxValue = myarray(i) <BR>&nbsp; &nbsp; &nbsp; &nbsp; maxElement = i <BR>&nbsp; &nbsp; &nbsp; &nbsp; maxCount = 1<BR>&nbsp; &nbsp; ElseIf maxValue = myarray(i) Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; maxCount = maxCount + 1 &#039 a tie<BR>&nbsp; &nbsp; End If<BR>Next <BR>%&#062;<BR><BR>After the loop: <BR><BR>maxValue is the maximal value found<BR>maxElement is number of the first array element with that value<BR>maxCount is the number of times the maximal value was found<BR><BR>************<BR><BR>Incidentally, not to complain too much, but this question is really a pretty basic one and so should have been asked in the ASP forum, I think, not here.<BR><BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Ooops...a goof...

The line:<BR><BR>if x &#062; myarray(i) Then<BR><BR>should read<BR><BR>if x &#060; myarray(i) Then<BR><BR>As written, the code would return the minimum value in the array.<BR><BR>

7. Steve Cimino Guest

## RE: Ooops...a goof...

Sure is. Hopefully he figured that out.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•