Averages/Mode: Difference between revisions
Content added Content deleted
Line 1,610: | Line 1,610: | ||
} |
} |
||
Checkit |
Checkit |
||
</lang> |
|||
Using idea from BBC BASIC. Function GetMode return array. As Array get bigger function run slower exponential. Previous example using inventory, has linear response (double data, double time to run) |
|||
<lang M2000 Interpreter> |
|||
Function GetMode { |
|||
Local max% |
|||
if empty then =(,) : exit |
|||
dim a() |
|||
a()=Array([]) |
|||
n%=len(a()) |
|||
dim c%(n%) |
|||
For i%=0 to n%-2 |
|||
For j%=i%+1 to n%-1 |
|||
if a(i%)==a(j%) then c%(i%)++ |
|||
Next j% |
|||
If c%(i%)>max% Then max%=c%(i%) |
|||
Next i% |
|||
For i%=0 to n%-1 |
|||
If c%(i%) = max% Then Data a(i%) |
|||
Next i% |
|||
=[] |
|||
} |
|||
Print GetMode(1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17) 'print 6 |
|||
Dim A(5) |
|||
A(0)= 1, 2, 4, 4, 1 |
|||
m=A() |
|||
Print GetMode(!m) ' print 1 4 |
|||
</lang> |
</lang> |
||