Q:

Write a PHP program to find the largest value from first, last, and middle elements of a given array of integers of odd length (atleast 1)

0

Write a PHP program to find the largest value from first, last, and middle elements of a given array of integers of odd length (atleast 1)

All Answers

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

<?php
function test($numbers)
 { 

        $first = $numbers[0];
        $middle_ele = $numbers[sizeof($numbers) / 2];
        $last_ele = $numbers[sizeof($numbers) - 1];
        $max_ele = $first;

        if ($middle_ele > $max_ele)
          {
            $max_ele = $middle_ele;
          }

        if ($last_ele > $max_ele)
          {
            $max_ele = $last_ele;
          }

        return $max_ele;;
 }   

echo test([1]) . "\n";
echo test([1,2,9]) . "\n";
echo test([1,2,9,3,3]) . "\n";
echo test([1,2,3,4,5,6,7]) . "\n";
echo test([1,2,2,3,7,8,9,10,6,5,4]) . "\n";

Sample Output:

1
9
9
7
8

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