Q:

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

0

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

All Answers

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

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

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