Algoritma modus, mean, median pada pascal
Tugas praktikum Dasar Algoritma yang meliputi pencarian
program TP_nomor3;
uses crt;
var input,freq : array [1..100] of integer;
modus,median : integer;
mean : real;
i,j,n : integer;
k : integer;
begin
clrscr;
writeln ('masukkan banyak data : '); readln (n);
for i := 1 to n do
begin
write ('masukkan data : '); readln (input[i]);
end;
mean := 0;
for i := 1 to n do
begin
mean := mean + input[i];
freq[input[i]+1] := freq[input[i]+1] + 1;
end;
mean := mean/n;
modus:=1;
k := 0;
for i := 1 to 100 do
begin
if freq[modus] <= freq[i] then
modus := i;
if freq[i] <> 0 then
for j := 1 to freq[i] do
begin
k := k + 1;
input[k] := i-1;
if k = n div 2 then
median := k;
end;
end;
write ('Data Terurut : ');
for i:= 1 to n do
write(input[i], ' ');
writeln;
writeln ('max : ' , input[n]);
writeln ('min : ' , input[1]);
writeln ('mean : ' , mean:0:2);
writeln ('modus : ',modus-1);
writeln ('median : ', input[median]);
readln;
end.
uses crt;
var input,freq : array [1..100] of integer;
modus,median : integer;
mean : real;
i,j,n : integer;
k : integer;
begin
clrscr;
writeln ('masukkan banyak data : '); readln (n);
for i := 1 to n do
begin
write ('masukkan data : '); readln (input[i]);
end;
mean := 0;
for i := 1 to n do
begin
mean := mean + input[i];
freq[input[i]+1] := freq[input[i]+1] + 1;
end;
mean := mean/n;
modus:=1;
k := 0;
for i := 1 to 100 do
begin
if freq[modus] <= freq[i] then
modus := i;
if freq[i] <> 0 then
for j := 1 to freq[i] do
begin
k := k + 1;
input[k] := i-1;
if k = n div 2 then
median := k;
end;
end;
write ('Data Terurut : ');
for i:= 1 to n do
write(input[i], ' ');
writeln;
writeln ('max : ' , input[n]);
writeln ('min : ' , input[1]);
writeln ('mean : ' , mean:0:2);
writeln ('modus : ',modus-1);
writeln ('median : ', input[median]);
readln;
end.
No comments:
Post a Comment