创建一个求和函数,仅对向量的一部分求和
显然我需要一个求和函数,而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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
computeSum()
函数必须出现在源文件中的main()
函数上方,才能使其处于作用域内。另外,在您的computeSum()函数签名中,您没有为howMany和total变量指定类型。我猜它们应该是双倍howMany
和双倍总计
?Your
computeSum()
function must appear above yourmain()
function in the source file for it to be in scope. Also in yourcomputeSum()
function signature you haven't given types to thehowMany
andtotal
variables. I'm guessing they should bedouble howMany
anddouble total
?事实上,
accumulate
会“削减它”,使用只考虑正值的适当函子:In fact,
accumulate
will “cut it”, with an appropriate functor that only regards positive values:骑上我的爱好马:Boost Range 适配器。与我达到最佳点
输出:
使用 C++11
awesomeness1 spice:Getting on my hobby horse: Boost Range Adaptors. Hits the sweet point with me
Output:
With C++11
awesomeness1 spice: