RosettaCodeData/Task/Averages-Mode/XPL0/averages-mode.xpl0

29 lines
829 B
Plaintext

proc Mode(Size, Array); \Show the mode(s) of Array
int Size, Array;
int List, Count I, J, K, Max;
[List:= Reserve(Size*4); \4 bytes per integer
Count:= Reserve(Size*4);
K:= 0;
for I:= 0 to Size-1 do
[for J:= 0 to K-1 do
if List(J) = Array(I) then \item is in List
[Count(J):= Count(J)+1; \ so increment its count
J:= Size;
];
if J = K then \not already in List
[List(K):= Array(I);
Count(K):= 1;
K:= K+1;
];
];
Max:= 0; \find maximum count
for J:= 0 to K-1 do
if Count(J) > Max then
Max:= Count(J);
for J:= 0 to K-1 do \show Array item(s) with Max Count
if Count(J) = Max then
[IntOut(0, List(J)); ChOut(0, ^ )];
];
Mode(9, [1,2,3,4,5,5,5123,2,3])