AJ Design Engineering Software Home Page
  Home    Page 2    Page 3    Page 4    Page 5    Physics    Math    Fluid Mechanics    Investment Finance     Mortgage Calculator     Nursing


AJ PHP Software Source Code Statistics Library
Median


number median ( number_array )

Description:

median returns the median of a set of numbers. If the number of elements in odd then the median is the middle number. If even, the median is the mean of the two middle number.

Source Code:

function median ($a)
{
  //variable and initializations
  $the_median = 0.0;
  $index_1 = 0;
  $index_2 = 0;

  //sort the array
  sort($a);

  //count the number of elements
  $number_of_elements = count($a);

  //determine if odd or even
  $odd = $number_of_elements % 2;

  //odd take the middle number
  if ($odd == 1)
  {
    //determine the middle
    $the_index_1 = $number_of_elements / 2;

    //cast to integer
    settype($the_index_1, "integer");

    //calculate the median
    $the_median = $a[$the_index_1];
  }
  else
  {
    //determine the two middle numbers
    $the_index_1 = $number_of_elements / 2;
    $the_index_2 = $the_index_1 - 1;

    //calculate the median
    $the_median = ($a[$the_index_1] + $a[$the_index_2]) / 2;
  }

  return $the_median;
}

Example:

<?
include ("ajstatistics.php");
$a = array(0, 1, 2, 3, 4, 5);

$the_median = median($a);

echo "a[0] = $a[0]<br>";
echo "a[1] = $a[1]<br>";
echo "a[2] = $a[2]<br>";
echo "a[3] = $a[3]<br>";
echo "a[4] = $a[4]<br>";
echo "a[5] = $a[5]<br>";
echo "the_median = $the_median<br>";
?>


Run Example


binomial coefficient binomial coefficient
mean mean of a set of number
median median of a set of number
standard deviation population standard deviation of a population
standard deviation sample standard deviation of a sample
variance population variance of a population
variance sample variance of a sample

References - Books:

Subwoofer Box Software Downloads for Sealed, Ported and Bandpass
Salary Wage Pay Calculator - Jobs Career
Baby Infant Growth Charts - Weight Age Percentile
Dog Age Calculator - Human Age Converter
Fraction Calculator - Simplify Reduce
Engine Horsepower Calculator
Earned Value Project Management
Present Worth Economics Calculator
Constant Acceleration Motion Calculator
Statistics Equations Formula Calculator
Mortgage Payment Finance Calculator
Weight Loss Formulas Calculator
Body Mass Index BMI Calculator
Automobile Fuel Economy Calculator
Gasoline Diesel

Car Truck Tire Size Comparison Calculator
Paycheck and Overtime Rate Calculator
Sale Discount Calculator - Percent Off Price

AJ Design - Online Science Mathematics Engineering Software Programs | 2 | 3 | 4 | 5

by Jimmy Raymond

Technical Tools, Specifications, How to Guides, Reviews, Training, Applications, Examples, Tutorials, Answers, Test Review Resources, Analysis, Homework Solutions, Help, Data and Information for Engineers, Technicians, Teachers, Tutors, Researchers, K-12 Education, College and High School Students, Science Fair Projects and Scientists

Contact: aj@ajdesigner.com

Privacy Policy, Disclaimer and Terms

Copyright 2002-2011