Results 1 to 5 of 5

Thread: How to find max value?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    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.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to find max value?

    Quote 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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

Posting Permissions

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



Click Here to Expand Forum to Full Width