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)
<?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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer