
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.

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 > myarray(i) Then<BR> x = myarray(i)<BR> myelement = i<BR> End If<BR>Next

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

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

Answering both...
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><%<BR>maxValue = myarray(0) <BR>maxElement = 0 <BR>maxCount = 0<BR><BR>For i = 1 to UBound(myArray) <BR> If maxValue < myarray(i) Then <BR> maxValue = myarray(i) <BR> maxElement = i <BR> maxCount = 1<BR> ElseIf maxValue = myarray(i) Then<BR> maxCount = maxCount + 1 ' a tie<BR> End If<BR>Next <BR>%><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>

Ooops...a goof...
The line:<BR><BR>if x > myarray(i) Then<BR><BR>should read<BR><BR>if x < myarray(i) Then<BR><BR>As written, the code would return the minimum value in the array.<BR><BR>

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

Forum Rules

