找到向量 c++ 输入的平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28574346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find average of input to vector c++
提问by alfiej12
I am trying to write a program where the user inputs as many numbers as they want and then the program returns the average of the numbers. So far the program only outputs the last number entered.
我正在尝试编写一个程序,用户可以输入任意数量的数字,然后程序返回数字的平均值。到目前为止,程序只输出最后输入的数字。
#include <vector>
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
vector<float> v;
int input;
cout << " Please enter numbers you want to find the mean of:" <<endl;
while (cin >> input);
v.push_back(input);
float average = accumulate( v.begin(), v.end(), 0.0/ v.size());
cout << "The average is" << average << endl;
return 0;
}
回答by P0W
First get rid of semi-colon after while
先去掉分号后 while
while (cin >> input);
~~
Second you math is wrong
其次你数学错了
third argument to std::accumulate
is initialvalue of sum
第三个参数std::accumulate
是sum 的初始值
Instead do:
而是这样做:
auto n = v.size();
float average = 0.0f;
if ( n != 0) {
average = accumulate( v.begin(), v.end(), 0.0) / n;
}
Also, the element of container data type shouldmatch the container type i.e. float
此外,容器数据类型的元素应与容器类型相匹配,即float
use float input ;
用 float input ;
回答by swang
There are quite a few bugs in your code, have you actually debugged it? here's a working version:
你的代码有不少bug,你真的调试过吗?这是一个工作版本:
#include <vector>
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
vector<float> v;
float input;
cout << " Please enter numbers you want to find the mean of:" <<endl;
while (cin >> input)
v.push_back(input);
float average = accumulate( v.begin(), v.end(), 0.0)/v.size();
cout << "The average is" << average << endl;
return 0;
}
回答by Some programmer dude
The third argument to std::accumulate
is the initialvalue, so you start with 0.0 / v.size()
(which is very small) and then add all items in the vector.
to的第三个参数std::accumulate
是初始值,因此您从0.0 / v.size()
(非常小)开始,然后添加向量中的所有项。
Instead you should use a value of zero as initial value, and after you calculated the total of all values in the vector thenyou divide by the size.
相反,您应该使用零值作为初始值,在计算向量中所有值的总和后,然后除以大小。
And as others pointed out, you only add the last value to the vector.
正如其他人指出的那样,您只需将最后一个值添加到向量中。
回答by Sarvesh
You can use vector_name.size() to get the number of elements in vector. Here's my attempt for to compute average:
您可以使用 vector_name.size() 获取向量中的元素数。这是我计算平均值的尝试:
#include <iostream>
#include <vector>
//function to compute average
double compute_average(std::vector<int> &vi) {
double sum = 0;
// iterate over all elements
for (int p:vi){
std::cout << "p is " << p << std::endl;
sum = sum + p;
}
return (sum/vi.size());
}
int main(){
std::vector <int>vi = {5,10};
double val;
val = compute_average(vi);
std::cout << "avg is " << val << std::endl;
return 0;
}