求向量 C++ 输入的平均值
find average of input to vector c++
我正在尝试编写一个程序,让用户输入任意数量的数字,然后程序 returns 计算这些数字的平均值。到目前为止,程序只输出最后输入的数字。
#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;
}
你的代码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;
}
在 C++17 及更高版本 中,您应该更喜欢 std::reduce
over std::accumulate
。两者都计算总和,但 std::reduce
具有更高的数值稳定性,而且不太重要的是,它可能更快,因为它可能是并行化的。
#include <vector>
#include <numeric>
#include <iostream>
float average(std::vector<float> const& v){
if(v.empty()){
return 0;
}
auto const count = static_cast<float>(v.size());
return std::reduce(v.begin(), v.end()) / count;
}
int main(){
std::vector<float> v{8, 4, 2, 7, 5};
auto const a = average(v);
std::cout << "average: " << a << "\n";
}
请注意,static_cast
在这里抑制了编译警告,即 std::size_t
(v.size()
的类型)具有 8 字节大小,理论上可以表达 4 字节精度的值float
不够。
您的代码有两个错误。先去掉while
后的分号
while (cin >> input); // <-- bug
v.push_back(input); // not in loop
// fixed loop
while (cin >> input){
v.push_back(input);
}
其次你的平均值计算有误。 std::accumulate
的第三个参数是sum的初值,默认为0
,不需要传[=27] =]
您的实际错误是,您将 0 除以元素计数。您想要的是将值总和除以元素计数。
float average = accumulate(v.begin(), v.end(), 0.0 / v.size());
// ^^^^^^^^^^^^^^
// fixed code
float average = accumulate(v.begin(), v.end(), 0.0) / v.size();
// without explicit init value
float average = accumulate(v.begin(), v.end()) / v.size();
// C++17 version with higher numerical stability
float average = reduce(v.begin(), v.end()) / v.size();
此外,容器数据类型 的元素应该 匹配容器类型,或者如果您真的想读取整数并添加到 float
向量中,您应该使用static_cast
。庞大的语法不仅抑制了任何编译器警告,而且清楚地表明这不是疏忽,而是数据类型的故意差异。
vector<float> v; // float vector
int input; // integer input value
// ...
v.push_back(input); // add int value to float container
// an intentional type conversion should catch the eye
v.push_back(static_cast<float>(input));
std::accumulate
的第三个参数是 初始值 ,因此您从 0.0 / v.size()
(非常小)开始,然后添加所有项目向量。
相反,您应该使用零值作为初始值,在计算向量中所有值的总和后然后除以大小。
正如其他人指出的那样,您只需将最后一个值添加到向量中。
您可以使用 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;
}
我正在尝试编写一个程序,让用户输入任意数量的数字,然后程序 returns 计算这些数字的平均值。到目前为止,程序只输出最后输入的数字。
#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;
}
你的代码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;
}
在 C++17 及更高版本 中,您应该更喜欢 std::reduce
over std::accumulate
。两者都计算总和,但 std::reduce
具有更高的数值稳定性,而且不太重要的是,它可能更快,因为它可能是并行化的。
#include <vector>
#include <numeric>
#include <iostream>
float average(std::vector<float> const& v){
if(v.empty()){
return 0;
}
auto const count = static_cast<float>(v.size());
return std::reduce(v.begin(), v.end()) / count;
}
int main(){
std::vector<float> v{8, 4, 2, 7, 5};
auto const a = average(v);
std::cout << "average: " << a << "\n";
}
请注意,static_cast
在这里抑制了编译警告,即 std::size_t
(v.size()
的类型)具有 8 字节大小,理论上可以表达 4 字节精度的值float
不够。
您的代码有两个错误。先去掉while
while (cin >> input); // <-- bug
v.push_back(input); // not in loop
// fixed loop
while (cin >> input){
v.push_back(input);
}
其次你的平均值计算有误。 std::accumulate
的第三个参数是sum的初值,默认为0
,不需要传[=27] =]
您的实际错误是,您将 0 除以元素计数。您想要的是将值总和除以元素计数。
float average = accumulate(v.begin(), v.end(), 0.0 / v.size());
// ^^^^^^^^^^^^^^
// fixed code
float average = accumulate(v.begin(), v.end(), 0.0) / v.size();
// without explicit init value
float average = accumulate(v.begin(), v.end()) / v.size();
// C++17 version with higher numerical stability
float average = reduce(v.begin(), v.end()) / v.size();
此外,容器数据类型 的元素应该 匹配容器类型,或者如果您真的想读取整数并添加到 float
向量中,您应该使用static_cast
。庞大的语法不仅抑制了任何编译器警告,而且清楚地表明这不是疏忽,而是数据类型的故意差异。
vector<float> v; // float vector
int input; // integer input value
// ...
v.push_back(input); // add int value to float container
// an intentional type conversion should catch the eye
v.push_back(static_cast<float>(input));
std::accumulate
的第三个参数是 初始值 ,因此您从 0.0 / v.size()
(非常小)开始,然后添加所有项目向量。
相反,您应该使用零值作为初始值,在计算向量中所有值的总和后然后除以大小。
正如其他人指出的那样,您只需将最后一个值添加到向量中。
您可以使用 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;
}