stats

countif

Available under GPL (Free) and Commercial licence
get a GPL licence
COST (GBP)
this unit 1.79
sub units 0.18
add a commercial licence to your cart
0
viewed 2417 times and licensed 16 times
www.codecogs.com/d-ox/stats/countif.php

Calculates the number of elements in an array that satisfy a certain condition.

Other modules that are used by this module. We add these to the cart for you whenever you add this module, unless you already own enough licences for them. Click for details
Controller: CodeCogs    Contact Controller

Contents hide toc

Group Description

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.
Authors:
Lucian Bentea (September 2005)

Interface

#include <codecogs/stats/countif.h>

using namespace Stats;

template<class T> int countif (int n, T *data, T cmp, bool (*fctpnt)(const T&, const T&))
Calculates the number of elements in an array that satisfy a certain condition.
int countif (int n, int *data, const char *cmp)
Calculates the number of elements in an array of integers that satisfy a certain condition.

Function Documentation

 
template<class T> intcountifintn
T*data
Tcmp
bool(*fctpnt)(const T&, const T&)[function pointer] )
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:
#include <iostream>
#include <codecogs/stats/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 << Stats::countif<int>(12, x, 2, Stats::isEqual);
  std::cout << std::endl;
  std::cout << "The number of elements greater than 3 is: ";
  std::cout << Stats::countif<int>(12, x, 3, Stats::isGreater);
  std::cout << std::endl;
  std::cout << "The number of elements less than 7 is: ";
  std::cout << Stats::countif<int>(12, x, 7, Stats::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: 10
Parameters:
nthe number of elements in the data array
datathe input array
cmpthe value which is compared to all values in the input array through the predicate function
fctpntthe predicate function
Returns:
the number of elements in the data array that satisfy the given predicate
Source Code:
Register

- To get code register with CodeCogs. Already a Member, then Login.


Countif Calculator

  

Add calculator to website or email
 
intcountifintn
int*data
const char*cmp )
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:
#include <iostream>
#include <codecogs/stats/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 << Stats::countif(12, x, "=2");
  std::cout << std::endl;
  std::cout << "The number of elements greater than 3 is: ";
  std::cout << Stats::countif(12, x, ">3");
  std::cout << std::endl;
  std::cout << "The number of elements less than 7 is: ";
  std::cout << Stats::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
Parameters:
nthe number of elements in the data array
datathe input array of integers
cmpa string expression giving the condition that an element of the data array should satisfy[const]
Returns:
the number of elements in the data array that satisfy the given condition
Source Code:
Register

- To get code register with CodeCogs. Already a Member, then Login.


Page Comments

  You must login to leave a messge


Last Modified: 18 Oct 07 @ 17:07     Page Rendered: 2008-08-21 02:01:39

Valid CSS!   Valid XHTML 1.0 Transitional