none
How t find the Mode(Most common number) in an Array?

    Question

  • How do I find the mode of an array? I am completely lost. All I have so far is

    Sub Mode
      For a = 1 To Listsize
        For j = 1 To maximum-1
          While Array[a] = Array[a+1] 
            counter = counter + 1
            If Array[a]<> Array[counter] Then
              Answer = Array[counter]
              TextWindow.WriteLine("The Mode is " + Answer)
            EndIf
          EndWhile
            If Array[a]<> Array[a+1] Then
              TextWindow.WriteLine("There is no mode")
            EndIf
          EndFor
        EndFor
      EndSub

    And I am certain it is wrong.

    Monday, September 23, 2013 11:27 PM

Answers

  • Hello Arbucks,

    How about this. I just tested it and it appears to work:

     Sub Mode
            Occurence = 0
            Top  = 0
            TopOccurence = 0
            X =0
            Y =0
            number[1] = 2
            number[2] = 3
            number[3] = 3
            number[4] = 2
            number[5] = 3
            For X = 1 To Array.GetItemCount(number)
                For Y = 1 To Array.GetItemCount(number)
                    If number[x] = number[y] Then
                        Occurence = Occurence + 1
                    EndIf
                endfor
                If Occurence > TopOccurence Then
                    Top = number[x]
                    TopOccurence = Occurence
                EndIf
                Occurence = 0
            endfor
            Textwindow.WriteLine("The MODE is: "+Top)
            textwindow.WriteLine("Number of times it occurs: " +TopOccurence)
           
        EndSub

    Tuesday, September 24, 2013 1:22 AM
  • I would:

    1. sort the array ascending or descending

    eg. {1,1,1,2,3,3,3,3,3,4,4,5,5,5,5,6,7,7,7,7,7,7,7,7,8,8,8,9,9}

    2. loop through the array from Index 1 to last-1,

    while comparing every value with the next one, next one, ..  until it's different (and counting equals into a 2nd array)

    3. take the maximum then of the 2nd array = most common num

    Tuesday, September 24, 2013 1:23 AM
  • How about this? 

    for I= 1 To 2000
      Dat[I]= Math.GetRandomNumber(1000)/10     '  only data making  ##.#
    EndFor

    For i=1 To 2000
      J= math.Round(Dat[i])
      LinearDat[J]=LinearDat[J]+1                        '  frequency  count up
    EndFor

    GraphicsWindow.BackgroundColor="Lightcyan"
    GraphicsWindow.PenColor="Red"
    GraphicsWindow.PenWidth=5
    GraphicsWindow.DrawRectangle(50,50,500,400)
    For I=1 To 8
      GraphicsWindow.DrawLine(50,50+50*(i-1),40,50+50*(i-1))
    EndFor
    min=100
    max=0
    For i=1 To 100
      GraphicsWindow.DrawLine(50+i*5,450,50+i*5,450- 10*LinearDat[i])  '  graph
      If LinearDat[i]<  min Then
        min=LinearDat[i]
      ElseIf max <LinearDat[i] then
        max=LinearDat[i]
      EndIf
    EndFor
    TextWindow.WriteLine(min+ "  :  "+max)

    For j=min To max
      For i=1 To 100
        If LinearDat[i]=J Then
          mode[j]=mode[j]+i+";"
        endif
      EndFor
      TextWindow.WriteLine("Frequency "+j+"  = "+mode[j])
    EndFor

    Tuesday, September 24, 2013 12:02 PM

All replies

  • Hello Arbucks,

    How about this. I just tested it and it appears to work:

     Sub Mode
            Occurence = 0
            Top  = 0
            TopOccurence = 0
            X =0
            Y =0
            number[1] = 2
            number[2] = 3
            number[3] = 3
            number[4] = 2
            number[5] = 3
            For X = 1 To Array.GetItemCount(number)
                For Y = 1 To Array.GetItemCount(number)
                    If number[x] = number[y] Then
                        Occurence = Occurence + 1
                    EndIf
                endfor
                If Occurence > TopOccurence Then
                    Top = number[x]
                    TopOccurence = Occurence
                EndIf
                Occurence = 0
            endfor
            Textwindow.WriteLine("The MODE is: "+Top)
            textwindow.WriteLine("Number of times it occurs: " +TopOccurence)
           
        EndSub

    Tuesday, September 24, 2013 1:22 AM
  • I would:

    1. sort the array ascending or descending

    eg. {1,1,1,2,3,3,3,3,3,4,4,5,5,5,5,6,7,7,7,7,7,7,7,7,8,8,8,9,9}

    2. loop through the array from Index 1 to last-1,

    while comparing every value with the next one, next one, ..  until it's different (and counting equals into a 2nd array)

    3. take the maximum then of the 2nd array = most common num

    Tuesday, September 24, 2013 1:23 AM
  • How about this? 

    for I= 1 To 2000
      Dat[I]= Math.GetRandomNumber(1000)/10     '  only data making  ##.#
    EndFor

    For i=1 To 2000
      J= math.Round(Dat[i])
      LinearDat[J]=LinearDat[J]+1                        '  frequency  count up
    EndFor

    GraphicsWindow.BackgroundColor="Lightcyan"
    GraphicsWindow.PenColor="Red"
    GraphicsWindow.PenWidth=5
    GraphicsWindow.DrawRectangle(50,50,500,400)
    For I=1 To 8
      GraphicsWindow.DrawLine(50,50+50*(i-1),40,50+50*(i-1))
    EndFor
    min=100
    max=0
    For i=1 To 100
      GraphicsWindow.DrawLine(50+i*5,450,50+i*5,450- 10*LinearDat[i])  '  graph
      If LinearDat[i]<  min Then
        min=LinearDat[i]
      ElseIf max <LinearDat[i] then
        max=LinearDat[i]
      EndIf
    EndFor
    TextWindow.WriteLine(min+ "  :  "+max)

    For j=min To max
      For i=1 To 100
        If LinearDat[i]=J Then
          mode[j]=mode[j]+i+";"
        endif
      EndFor
      TextWindow.WriteLine("Frequency "+j+"  = "+mode[j])
    EndFor

    Tuesday, September 24, 2013 12:02 PM