Returns the median of the given population.

You're viewing an older version of this page (#106). View the current version.

View versions (1)

Interface

#include <codecogs/statistics/moments/median.h>

using namespace Statistics::Moments;

The median is the number in the middle of a set of numbers; that is, half the numbers have values that are greater than the median, and half have values that are less. If there is an even number of numbers in the set, then it calculates the average of the two numbers in the middle. In order to calculate the median, this algorithm uses the Smallest function.

Parameters

n
the size of the population
data
the actual population given as an array

Returns

the median of the population

Example:

#include <codecogs/statistics/moments/median.h>
#include <iostream>
int main()
{
  double
  x[6] = {0.2, 1.24, 0.5, 2.54, 2.3, 0.3},
  y[6] = {1, 2, 3.2, 4, 5},
  medx = Stats::Moments::median<double>(6, x),
  medy = Stats::Moments::median<double>(5, y);
  std::cout << "The median of the x array is: " << medx << std::endl;
  std::cout << "The median of the y array is: " << medy << std::endl;
  return 0;
}

Output:

The median of the x array is: 0.87
The median of the y array is: 3.2

References

SUBSET, a library of C++ combinatorial routines, http://www.csit.fsu.edu/~burkardt/cpp_src/subset/subset.html

GPL Licence — free for non commercial use. See Licence details.