Q:

PHP program to find the maximum and minimum element of an array

belongs to collection: PHP Array Programs

0

The program is to find the minimum and the maximum element of the array can be solved by two methods. This program can be practically used.

For example, a teacher has an array of marks of students and needs to declare the topper of the class. So the given program will easily give the teacher maximum marks in the array.

All Answers

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

Program:

<?php
// Find maximum in array
function getMaxElement($array)
{
    $n = count($array);
    $max = $array[0];
    for ($i = 1;$i < $n;$i++) if ($max < $array[$i]) $max = $array[$i];
    return $max;
}

// Find maximum in array
function getMinElement($array)
{
    $n = count($array);
    $min = $array[0];
    for ($i = 1;$i < $n;$i++) if ($min > $array[$i]) $min = $array[$i];
    return $min;
}

// Main code
$array = array(
    10,
    20,
    30,
    40,
    50
);
echo (getMaxElement($array));
echo ("\n");
echo (getMinElement($array));
?>

Output:

50
10

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

total answers (1)

Appending/merging of two arrays in PHP... >>