Code to Calculate Modal Value

Discussion in 'Questions (Windows Mobile)' started by Zenerdiode, Nov 23, 2008.

  1. Zenerdiode

    Zenerdiode Active Member Licensed User

    I have a routine that tests a mechanical timer a number of times. I would like to display the Mean, Modal, Median, Maximum and Minimum times.

    I sort the ArrayList before using the subs. I'm struggling with the code to calculate the modal value. I appreciate in my dataset there may be (rarely) more than one modal value.

    Code:
    #Region StatsCalculators
    Sub CalcMean
       
    For i=0 To ArrayList1.Count-1
          x=x+ArrayList1.Item(i)
       
    Next
       
    Return Format(x/ArrayList1.Count,"F2")
    End Sub

    Sub CalcMode
       
    Return "Uniform Dist." 'Unless there is a genuine Modal value.
    End Sub

    Sub CalcMed
       
    If ArrayList1.Count Mod 2=1 Then
          
    Return ArrayList1.Item(((ArrayList1.Count+1)/2)-1)
       
    Else
          
    Return Format((ArrayList1.Item((ArrayList1.Count/2)-1)+ArrayList1.Item(ArrayList1.Count/2))/2,"F2")
       
    End If
    End Sub

    Sub CalcMax
       
    Return ArrayList1.Item(ArrayList1.Count-1)
    End Sub

    Sub CalcMin
       
    Return ArrayList1.Item(0)
    End Sub
    #End Region
     
  2. klaus

    klaus Expert Licensed User

    In the joined program you have an example to calculate multiple modal values.

    Best regards.

    Edit: a new version optimized, I left the previous routine in the program.
     
    Last edited: Nov 24, 2008
  3. Zenerdiode

    Zenerdiode Active Member Licensed User

    Thanks klaus,

    That's exactly what I was wanting. :)

    *EDIT* - Not quite. If the dataset array does not have a modal value (i.e. a Uniform Distribution) the sub errors. Also, a dataset of, say, (2,3,3) reports '1*2' rather than '2*3'
     
    Last edited: Nov 24, 2008
  4. klaus

    klaus Expert Licensed User

    Hi Zenerdiode,

    Here is an updated version of the program that solves your problem.
    Sorry for the mistake.

    Best regards.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice