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

  • برمجة بي اتش بي

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
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...