AJ PHP Software Source Code Math Library
Array Average
float array_average ( array_number a)
Description:
array_average calculates the mean or average of an array of numbers.
Source Code:
function array_average ($a)
{
//variable and initializations
$the_result = 0.0;
$the_array_sum = array_sum($a); //sum the elements
$number_of_elements = count($a); //count the number of elements
//calculate the result
$the_result = $the_array_sum / $number_of_elements;
//return the value
return $the_result;
} |
Example:
<?
include ("ajmath.php"); $a = (2.0, 3.0, 5.0);
$the_average = array_average($a);
echo "a[0] = $a[0]<br>";
echo "a[1] = $a[1]<br>";
echo "a[2] = $a[2]<br>";
echo "the_average = $the_average<br>";
?>
|
Run Example
References - Books:
Max A. Sobel, Nobert Lerner. 1991. Precalculus Mathematics. Prentice Hall.