|
-
Nov 8th, 2008, 10:05 AM
#1
Thread Starter
Addicted Member
How to find max value?
Dear all expert programmer,
If I have data below
Sample1
0=20%
1=0%
2=10%
3=90%
4=50%
5=50%
6=70%
8=20%
9=90%
Max value is 39
Sample2
0=60%
1=100%
2=10%
3=90%
4=50%
5=50%
6=70%
8=20%
9=90%
Max value is 1
Please tell me how to find max value.
Thank you for all answer.
-
Nov 8th, 2008, 10:17 AM
#2
Re: How to find max value?
Put your items in an array and use a For:Next loop to iterate thru the array. Start your MaxValue at 0 and each time a value in the array is > MaxValue, set MaxValue to that array item's value. When the loop ends, MaxValue will have the highest value.
Additionally, if the absolute Max can only be 100, then after you set a new MaxValue within the loop, and it is 100, you can then abort the loop early (i.e., Exit For) because no other value in the array can be > 100. Exception: you didn't mention what happens if more than one value can ultimately be the MaxValue.
Last edited by LaVolpe; Nov 8th, 2008 at 10:21 AM.
-
Nov 8th, 2008, 12:00 PM
#3
Re: How to find max value?
Appending to LaVolpe's post, if you need to know how many times the maximum value occurs, you will need to have a frequency counter that increments whenever that value is detected. The counter restarts when a new maximum value is detected as it loops through the list.
If you need to know the list positions where the maximum value occurs, that has to be tracked in an array. In your examples, what I believe you meant to say was that the maximum value occurs at positions 3 and 9 for the first list and at position 1 for the second.
-
Nov 8th, 2008, 12:55 PM
#4
Re: How to find max value?
 Originally Posted by Code Doc
In your examples, what I believe you meant to say was that the maximum value occurs at positions 3 and 9 for the first list and at position 1 for the second.
Good catch! I was really confused as to what 39 was. I was thinking that 100% was in the 39th position and the user just didn't post all 39+ values.
-
Nov 8th, 2008, 02:31 PM
#5
Re: How to find max value?
Build a form with a label and a command button. Then apply this code:
Code:
Private Sub Command1_Click()
Dim MyData() As String, MaxValue As String
MyData = Split("20%,0%,10%,90%,50%,50%,70%,80%,20%,90%", ",")
MaxValue = MyData(0)
For I = 1 To UBound(MyData)
If MyData(I) > MyData(I - 1) Then MaxValue = MyData(I)
Next
Label1.Caption = vbNullString
Label1.Caption = "Maximum Value is " & MaxValue & vbCrLf
For I = 0 To UBound(MyData)
If MaxValue = MyData(I) Then Label1.Caption = Label1.Caption & "Located at Position " & I & vbCrLf
Next
End Sub
Note: OP left out a data point at position 7, so I added in 80% there to help keep the ship afloat.
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
|
Click Here to Expand Forum to Full Width
|