Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now