countif
Calculates the number of elements in an array that satisfy a certain condition.
Interface
#include <codecogs/statistics/countif.h>
using namespace Statistics;
Overview
The components in this module calculate the number of elements in an array that satisfy a certain condition. The difference between the two functions consists in the way the condition is given.
FUNCTION
countif
This function calculates the number of elements in an array that satisfy a certain predicate given as argument. The predicate is a user-defined function that takes as first argument each element of the array and as second argument the value of cmp . Three predicate functions are available with this module to provide basic relational operators, tests to see if two values are equal, or whether one is greater/lesser than the other.
Example 1
#include <iostream>
#include <codecogs/statistics/countif.h>
int main()
{
int x[12] = {3, 5, 1, 2, 6, 8, 10, 2, 2};
std::cout << "The number of elements equal to 2 is: ";
std:: cout << Statistics::countif<int>(12, x, 2, Statistics::isEqual);
std::cout << std::endl;
std::cout << "The number of elements greater than 3 is: ";
std::cout << Statistics::countif<int>(12, x, 3, Statistics::isGreater);
std::cout << std::endl;
std::cout << "The number of elements less than 7 is: ";
std::cout << Statistics::countif<int>(12, x, 7, Statistics::isLess);
std::cout << std::endl;
return 0;
}Output
The number of elements equal to 2 is: 3
The number of elements greater than 3 is: 4
The number of elements less than 7 is: 10Parameters
Returns
FUNCTION
countif
This function calculates the number of integers in an array that satisfy a certain predicate given as argument. The predicate is given as a character string, with the following syntax: the first character is one of "=", "!", ">" or "<" to provide the relation that needs to be satisfied, where "!" means inequality. The next characters are used to specify the integer (signed or unsigned) to which the value of each element of the array is to be compared.
Example 1
#include <iostream>
#include <codecogs/statistics/countif.h>
int main()
{
int x[12] = {3, 5, 1, 2, 6, 8, 10, 2, 2};
std::cout << "The number of elements equal to 2 is: ";
std::cout << Statistics::countif(12, x, "=2");
std::cout << std::endl;
std::cout << "The number of elements greater than 3 is: ";
std::cout << Statistics::countif(12, x, ">3");
std::cout << std::endl;
std::cout << "The number of elements less than 7 is: ";
std::cout << Statistics::countif(12, x, "<7");
std::cout << std::endl;
return 0;
}Output
The number of elements equal to 2 is: 3
The number of elements greater than 3 is: 4
The number of elements less than 7 is: 10