viewed 2180 times and licensed 10 times
Calculates the sum of elements in an array that satisfy a certain condition.
View other versions (2)
Contents  |
|
Group Description
The components available with this module calculate the sum 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/sumif.h>
using namespace Stats;
| template<class T> T | sumif (int n, T *data, T cmp, bool (*fctpnt)(const T&, const T&)) Calculates the sum of elements in an array that satisfy a certain condition. |
| int | sumif (int n, int *data, const char *cmp) Calculates the sum of elements in an array of integers that satisfy a certain condition. |
Function Documentation
This function calculates the sum 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/stats/sumif.h>
int main()
{
int x[12] = {3, 5, 1, 2, 6, 8, 10, 2, 2};
std::cout << "The sum of elements equal to 2 is: ";
std::cout << Stats::sumif<int>(12, x, 2, Stats::isEqual);
std::cout << std::endl;
std::cout << "The sum of elements greater than 3 is: ";
std::cout << Stats::sumif<int>(12, x, 3, Stats::isGreater);
std::cout << std::endl;
std::cout << "The sum of elements less than 7 is: ";
std::cout << Stats::sumif<int>(12, x, 7, Stats::isLess);
std::cout << std::endl;
return 0;
}
Output:
The sum of elements equal to 2 is: 6
The sum of elements greater than 3 is: 29
The sum of elements less than 7 is: 21
Parameters:
| n | the number of elements in the data array |
| data | the input array |
| cmp | the value which is compared to all values in the input array through the predicate function |
| fctpnt | the predicate function |
Returns:
- the number of elements in the data array that satisfy the given predicate
This function calculates the sum 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 2:
#include <iostream>
#include <codecogs/stats/sumif.h>
int main()
{
int x[12] = {3, 5, 1, 2, 6, 8, 10, 2, 2};
std::cout << "The sum of elements equal to 2 is: ";
std::cout << Stats::sumif<int>(12, x, "=2");
std::cout << std::endl;
std::cout << "The sum of elements greater than 3 is: ";
std::cout << Stats::sumif<int>(12, x, ">3");
std::cout << std::endl;
std::cout << "The sum of elements less than 7 is: ";
std::cout << Stats::sumif<int>(12, x, "<7");
std::cout << std::endl;
return 0;
}
Output:
The sum of elements equal to 2 is: 6
The sum of elements greater than 3 is: 29
The sum of elements less than 7 is: 21
Parameters:
| n | the number of elements in the data array |
| data | the input array of integers |
| cmp | a string expression giving the condition that an element of the data array should satisfy |
Returns:
- the number of elements in the data array that satisfy the given condition
Page Comments
You must login to leave a messge
Last Modified: 18 Oct 07 @ 17:07 Page Rendered: 2010-03-10 23:20:11