Skip to main content
added 10 characters in body
Source Link

Create a single Sum function that can accept any kind of predicate. If you want to find the sum of all odd elements, pass IsOdd function to it as follows.

Sum(&IsOdd, data)

where data is a vector of int.

For more details, see the complete code below.

#include <iostream>
#include <vector>
#include <cmath>


typedef bool (*Predicate)(int);

int Sum(Predicate f, std::vector<int> data)
{
    size_t N = data.size();
    int sum = 0;
    for (int i = 0; i < N; ++i)
        if (f(data[i]))
            sum += data[i];
    return sum;
}

bool IsOdd(int x)
{
    return x % 2 == 1;
}

bool IsEven(int x)
{
    return x % 2 == 0;
}

bool IsPrime(int x)
{
    if (x < 2)
        return false;
    if (x == 2)
        return true;
    if (x % 2 == 0)
        return false;

    int N = (int)std::sqrt(x);
    for (int i = 2; i < N; i = i + 2)
        if (x % (i + 1) == 0)
            return false;

    return true;
}

int main()
{
    std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 49};
    std::cout << "The total sum of prime elements: " << Sum(&IsPrime, data) << "\n";
    return 0;
}

Create a single Sum function that can accept any kind of predicate. If you want to find the sum of all odd elements, pass IsOdd function to it as follows.

Sum(&IsOdd, data)

where data is a vector of int.

For more details, see the complete code below.

#include <iostream>
#include <vector>
#include <cmath>


typedef bool (*Predicate)(int);

int Sum(Predicate f, vector<int> data)
{
    size_t N = data.size();
    int sum = 0;
    for (int i = 0; i < N; ++i)
        if (f(data[i]))
            sum += data[i];
    return sum;
}

bool IsOdd(int x)
{
    return x % 2 == 1;
}

bool IsEven(int x)
{
    return x % 2 == 0;
}

bool IsPrime(int x)
{
    if (x < 2)
        return false;
    if (x == 2)
        return true;
    if (x % 2 == 0)
        return false;

    int N = (int)std::sqrt(x);
    for (int i = 2; i < N; i = i + 2)
        if (x % (i + 1) == 0)
            return false;

    return true;
}

int main()
{
    vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 49};
    std::cout << "The total sum of prime elements: " << Sum(&IsPrime, data) << "\n";
    return 0;
}

Create a single Sum function that can accept any kind of predicate. If you want to find the sum of all odd elements, pass IsOdd function to it as follows.

Sum(&IsOdd, data)

where data is a vector of int.

For more details, see the complete code below.

#include <iostream>
#include <vector>
#include <cmath>


typedef bool (*Predicate)(int);

int Sum(Predicate f, std::vector<int> data)
{
    size_t N = data.size();
    int sum = 0;
    for (int i = 0; i < N; ++i)
        if (f(data[i]))
            sum += data[i];
    return sum;
}

bool IsOdd(int x)
{
    return x % 2 == 1;
}

bool IsEven(int x)
{
    return x % 2 == 0;
}

bool IsPrime(int x)
{
    if (x < 2)
        return false;
    if (x == 2)
        return true;
    if (x % 2 == 0)
        return false;

    int N = (int)std::sqrt(x);
    for (int i = 2; i < N; i = i + 2)
        if (x % (i + 1) == 0)
            return false;

    return true;
}

int main()
{
    std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 49};
    std::cout << "The total sum of prime elements: " << Sum(&IsPrime, data) << "\n";
    return 0;
}
Source Link

Create a single Sum function that can accept any kind of predicate. If you want to find the sum of all odd elements, pass IsOdd function to it as follows.

Sum(&IsOdd, data)

where data is a vector of int.

For more details, see the complete code below.

#include <iostream>
#include <vector>
#include <cmath>


typedef bool (*Predicate)(int);

int Sum(Predicate f, vector<int> data)
{
    size_t N = data.size();
    int sum = 0;
    for (int i = 0; i < N; ++i)
        if (f(data[i]))
            sum += data[i];
    return sum;
}

bool IsOdd(int x)
{
    return x % 2 == 1;
}

bool IsEven(int x)
{
    return x % 2 == 0;
}

bool IsPrime(int x)
{
    if (x < 2)
        return false;
    if (x == 2)
        return true;
    if (x % 2 == 0)
        return false;

    int N = (int)std::sqrt(x);
    for (int i = 2; i < N; i = i + 2)
        if (x % (i + 1) == 0)
            return false;

    return true;
}

int main()
{
    vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 49};
    std::cout << "The total sum of prime elements: " << Sum(&IsPrime, data) << "\n";
    return 0;
}