Write a PHP program to compute the difference between the largest and smallest values in a given array of integers and length one or more
<?php function test($nums) { $small_num = 0; $biggest_num = 0; if (sizeof($nums) > 0) $small_num = $biggest_num = $nums[0]; for ($i = 1; $i < sizeof($nums); $i++) { $small_num = min($small_num, $nums[$i]); $biggest_num = max($biggest_num, $nums[$i]); } return $biggest_num - $small_num; } echo "Difference between the largest and smallest values: ". test([1, 5, 7, 9, 10, 12] );
Sample Output:
Difference between the largest and smallest values: 11
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