PHP Menghitung Median (Nilai Tengah)

<?php
$bil = array (9, 2, 10, 3, 7, 5, 2);

$i = 0;
echo “Data belum urut<br>”;
while(list($indeks,$nilai)=each($bil))
{
echo “$nilai  “;
$i=$i+1;
}
echo “<br>”;

$bil_urut=$bil;
asort($bil_urut);
$i = 0;
echo “Data telah urut<br>”;
while(list($indeks,$bil[$i])=each($bil_urut))
{
echo “$bil[$i] “;
$i=$i+1;
}
echo “<br>”;

$tengah=(count($bil)-1)/2;
if ((count($bil)-1)>1)
{
if ((count($bil)-1)%2==0)
{
$median=($bil[$tengah-1]+$bil[$tengah])/2;
}
else
{
$median=$bil[$tengah];
}
}
else
{
$median= ” tidak ada”;
}
echo “Mediannya adalah”.” = “.$median;
?>

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: