创建一个求和函数,仅对向量的一部分求和

发布于 2024-12-05 14:11:03 字数 1420 浏览 1 评论 0原文

显然我需要一个求和函数,而accumulate不会削减它

我需要创建程序-一个向量-用户可以指定n个元素-并且求和函数只能对正元素求和,即使用户可以输入负元素还有......

在computeSum函数中,我还需要向整个组添加一个“成功”

computeSum (dataVec, howMany, total, sucess);

,并为输入的人创建一个参数 - 所有负数,但想要对它们求和,但无法,因为没有正数

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

所以这是我得到的

#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;
 }

我似乎也遇到了麻烦编译这个 -computeSum() 在 int main() 中不被理解; computerSum() 中未理解 howMany;在全球范围内,total() 和 howMany() 未声明(我想这意味着我需要全局声明???)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮生面具三千个 2024-12-12 14:11:04

您的 computeSum() 函数必须出现在源文件中的 main() 函数上方,才能使其处于作用域内。另外,在您的computeSum()函数签名中,您没有为howMany和total变量指定类型。我猜它们应该是双倍howMany双倍总计

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?

朱染 2024-12-12 14:11:03

事实上,accumulate“削减它”,使用只考虑正值的适当函子:

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

…

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

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);
懒猫 2024-12-12 14:11:03

骑上我的爱好马:Boost Range 适配器。与我达到最佳点

#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;
}

输出:

总和:24826


使用 C++11 awesomeness1 spice

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

1:说实话,我真的很讨厌lambda语法的笨拙:

  • 必须始终指定参数类型
  • 必须将 return 语句拼出
    对于这种情况,似乎
    filtered([] (i) { i>=0 })
    可以被编译器算出来。好吧,也许在 c++22 中:)

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 :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文