Write a PHP program to find the larger average value between the first and the second half of a given array of integers and minimum length is atleast 2. Assume that the second half begins at index (array length)/2
- برمجة بي اتش بي
- 2021-09-08
- mhanasmh00489829403
الأجوبة
<?php
function test($numbers)
{
$firstHalf = Average($numbers, 0, sizeof($numbers) / 2);
$secondHalf = Average($numbers, sizeof($numbers) / 2, sizeof($numbers));
return $firstHalf > $secondHalf ? $firstHalf : $secondHalf;
}
function Average($num, $start, $end)
{
$sum = 0;
for ($i = $start; $i < $end; $i++)
$sum += $num[$i];
return $sum / (sizeof($num) / 2);
}
echo test([1, 2, 3, 4, 6, 8])."\n";
echo test([15, 2, 3, 4, 15, 11])."\n";
Sample Output:
6 10
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال