# sumif

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

Controller: **CodeCogs**

**Contents**

## Dependents

## Interface

C++

## Overview

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)*

## Sumif

template<class T> Tsumif( | int | n | |

T* | data | ||

T | cmp | ||

bool | (*fctpnt)(const T&, const T&)[function pointer] | ) |

### Example 1

#include <iostream> #include <codecogs/statistics/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

##### Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

## Sumif

intsumif( | int | n | |

int* | data | ||

const char* | cmp | ) |

### Example 2

#include <iostream> #include <codecogs/statistics/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

##### Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

Last Modified: 19 Dec 11 @ 00:38 Page Rendered: 2022-03-14 11:47:14