【发布时间】:2011-09-18 17:59:55
【问题描述】:
显然我需要一个 sum 函数,并且累积不会削减它
我需要创建程序 - 一个向量 - 用户可以指定 n 个元素 - 并且 sum 函数只能对正元素求和,即使用户也可以输入负元素...
在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() 未声明(我想这意味着我需要在全局范围内进行 decalre ???)
【问题讨论】: