Files
G4G0-2/Client Server Systems/Week 3/workshop3/operators.php
2024-10-16 09:12:37 +01:00

34 lines
897 B
PHP

<?php
$temp = array(78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76, 73, 68, 62, 73, 72, 65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 73);
function smallestTemp($temp)
{
sort($temp, SORT_NATURAL);
for ($i = 0; $i < 7; $i++) {
$smallestTemp[$i] = $temp[$i];
}
return implode(", ", $smallestTemp);
}
function largestTemp($temp) {
rsort($temp, SORT_NATURAL);
for ($i = 0; $i < 7; $i++) {
$largestTemp[$i] = $temp[$i];
}
return implode(", ", $largestTemp);
}
function calcAvgTemp($temp) {
$totalTemp = 0;
foreach ($temp as $value) {
$totalTemp += $value;
}
return round( ($totalTemp / count($temp) ), 1 );
}
echo "Average Temperature is : " . calcAvgTemp($temp);
echo "<br>";
echo "List of seven lowest temperatures : " . smallestTemp($temp);
echo "<br>";
echo "List of seven highest temperatures : " . largestTemp($temp);