Find Mean and Median

Posted on 21.06.2010

0


program find_mean_median(Output);
uses crt;
var
   x : array[1..1000] of integer;
   i, j, n, tmp, k : integer;
   mean, median : real;
   min, max : integer;
begin
   clrscr;
   write(‘banyak data : ‘); readln(n);

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

{ .:: Sort Value to Find Median ::. }
   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));

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

{ .:: Mean ::. }
   writeln(‘mean : ‘, (mean/n):4:2);

{ .:: Median ::. }
   if (n mod 2 = 0) then median:=(x[(n div 2)]+x[(n div 2)+1])/2
   else median:=x[(n div 2)+1];
   writeln(‘median : ‘, median:3:2);

   readln;
end.

Posted in: Pascal Code