Calculates the rank of a number in a list of numbers.

View versions (1)

Interface

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

using namespace Statistics::Moments;

Returns the rank of a number in a list of numbers. The rank of a number is its size relative to other values in a list. (If you were to sort the list, the rank of the number would be its position.) Rank gives duplicate numbers the same rank. However, the presence of duplicate numbers affects the ranks of subsequent numbers. For example, in a list of integers sorted in ascending order, if the number 10 appears twice and has a rank of 5, then 11 would have a rank of 7 (no number would have a rank of 6).

Example 1

#include <codecogs/statistics/moments/rank.h>
#include <iostream>
int main()
{
  double x[5] = {4 , 5 , 8 , 6 , 3};
  double rka = Stats::Moments::rank(4, 5, x);
  double rkd = Stats::Moments::rank(4, 5, x, false);
  std::cout << "The rank of 4 in the array x sorted in ascending order is: " << rka << std::endl;
  std::cout << "The rank of 4 in the array x sorted in descending order is: " << rkd << std::endl;
  return 0;
}

Output:

The rank of 4 in the array x sorted in ascending order is: 2
The rank of 4 in the array x sorted in descending order is: 4

Parameters

x
is the number whose rank you want to find
n
the size of the array
data
the actual array data
ascending
Default Value = true

Returns

the rank of a number in a list of numbers
GPL Licence — free for non commercial use. See Licence details.