creating a sum function for summing only part of a

2019-09-02 11:14发布

问题:

Obviously I need a sum function for this and accumulate will not cut it

I need to create program - a vector - with n number of elements the user can prescribe - and the sum function can only sum POSITIVE elements even though the user can enter negative elements as well...

In the computeSum function I also need to add a "success" to the whole group

computeSum (dataVec, howMany, total, sucess);

and create a parameter for people who enter - all negative numbers but want to sum them but are unable to because there are no positive numbers

if (success) {
   cout << "The sum is " << total << endl;
}
else {
cerr << "Oops, you cannot add these elements.";
}

So here is what I got

#include <iostream>
#include <vector>        // need this in order to use vectors in the program
using namespace std;

int main()
{
    vector<double> dataVec;

    double i, n, howMany, total;
    cout << "How many numbers would you like to put into the vector?";
    cin >> n; 

    dataVec.resize(n);

    for(vector<double>::size_type i=0;i < n;i++)
    {
        cout << "Enter the numbers: \n";
        cin >> dataVec[i];
    }

    cout << "How many POSITIVE numbers would you like to sum?";
    cin >> howMany;
    cout << computeSum (dataVec, howMany, total);
}

 double computeSum (vector<double> &Vec, howMany, total)
 {
      double total =0;
      for(int i=0;i < howMany;i++)
    total+=Vec[i];
      return total;
 }

I also seem to having trouble compiling just this - computeSum() is not being understood in int main(); howMany is not being understood in computerSum(); and on a gloabl scope total() and howMany() are undeclared (I guess that would mean i would need to decalre globally???)

回答1:

In fact, accumulate will “cut it”, with an appropriate functor that only regards positive values:

int sum_positive(int first, int second) {
    return first + (second > 0 ? second : 0);
}

…

std::accumulate(data.begin(), data.begin() + how_many, 0, sum_positive);


回答2:

Getting on my hobby horse: Boost Range Adaptors. Hits the sweet point with me

#include <boost/range/adaptors.hpp>
#include <boost/range/numeric.hpp>

bool isnatural(int i) { return i>=0; }
using namespace boost::adaptors;

int main(int argc, char** args)
{
    static const int data[] = { -130, -1543, 4018, 5542, -4389, 15266, };

    std::cout << "sum: " << boost::accumulate(data | filtered(isnatural), 0) << std::endl;

    return 0;
}

Output:

sum: 24826


With C++11 awesomeness1 spice:

std::cout << "sum: " << boost::accumulate(data 
        | filtered([] (int i) { return i>=0; }), 0) << std::endl;

1: to be honest, I really hate the clumsyness of lambda syntax:

  • having to specify the parameter type always
  • having to spell out the return statement to For this scenario, it seems to that
    filtered([] (i) { i>=0 })
    could be figured out by the compiler. Well, perhaps in c++22 :)


回答3:

Your computeSum() function must appear above your main() function in the source file for it to be in scope. Also in your computeSum() function signature you haven't given types to the howMany and total variables. I'm guessing they should be double howMany and double total?



标签: c++ stl vector