Averages/Median: Difference between revisions

Content deleted Content added
m →‎{{header|Haskell}}: remove URL from libheader invocation
→‎{{header|PHP}}: Fix PHP example
Line 630:
 
=={{header|PHP}}==
This solution uses the sorting method of finding the median.
{{incorrect|PHP|What about (4,1,3,6,8)? Median != middle element.}}
<lang php>
function median($arr)
$arr = array(1,3,4,6,8); //initialize an array
{
sort($arr);
$count = count($arr); //count the number of values in array
$middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
Line 642 ⟶ 644:
$median = (($low+$high)/2);
}
echoreturn $median;
}
 
echo median(array(4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n"; // 4.4
echo median(array(4.1, 7.2, 1.7, 9.3, 4.4, 3.2)) . "\n"; // 4.25
</lang>