Find Modus

Posted on 22.06.2010

0


program find_modus(Output);
uses crt;
type
   jns_nilai = (banyak_duplikasi, mungkin_modus);
   cacahan_modus = array[1..1000, jns_nilai] of integer;
   int_array = array[1..1000] of integer;
var
   x : int_array;
   modus : cacahan_modus;
   i, j, n, tmp, k : integer;
   min, max : integer;
   count, tmpmodus : integer;

begin
   clrscr;
   write(‘banyak data : ‘); readln(n);

   randomize;
{ .:: Input data ::. }
   for i:=1 to n do begin
      x[i]:=random(100)+1;
      write(x[i]:4,’ ‘);
      if (i mod 15=0) then writeln;
   end;
   writeln;
   writeln;

{ .:: Sort Value to Find Modus ::. }
   j:=1; k:=n;
   repeat
      min:=x[j];
      for i:=(j+1) to k do
         if (x[i]max) then begin
            max:=x[i];
            tmp:=x[k]; x[k]:=max; x[i]:=tmp;
         end;

      j:=j+1; k:=k-1;
   until (j > (n div 2));

{ .:: Find value that repeat and save them ::. }
   j:=1; k:=1; max:=0;
   repeat
      tmp:=x[j]; count:=0; tmpmodus:=0;
      i:=j+1;
      while (x[i]=tmp) do begin
         count := count+1;
         tmpmodus := x[i];
         i := i+1;
      end;
      if (count0) then begin
         modus[k, banyak_duplikasi] := count;
         modus[k, mungkin_modus] := tmpmodus;
         k := k+1;
         if (count>max) then
            max:=count;
      end;
      j:=i;
   until (j>n);

{ .:: Show order data ::. }
   writeln(‘Data diurutkan ‘);
   for i:=1 to n do begin
      write(x[i]:4,’ ‘);
      if (i mod 15=0) then writeln;
   end;
   writeln;
   writeln;

{ .:: Modus Value ::. }
   write(‘Nilai Modus = ‘);
   if max0 then
      for i:=1 to (k-1) do begin
         if (modus[i, banyak_duplikasi]=max) then
            write(modus[i, mungkin_modus],’ ‘);
      end;

   writeln;
   writeln;

   readln;
end.

Posted in: Pascal Code