我想知道如何将一个浮点数组的所有元素相加,并使和浮点平均值;。我是否必须使用for循环,或者是否有其他方法将元素0与1相加,元素2与3相加,等等?
发布于 2011-10-05 22:40:24
您可以使用for循环,也可以使用std::accumulate。
#include <iostream>
#include <numeric>
int main()
{
float arr[17] = { 1, 2, 3, };
//Sum the array
const float sum = std::accumulate(arr, arr+17, 0.0 );
std::cout << "Sum: " << sum << "\n";
std::cout << "Average: " << sum/17 << "\n";
}发布于 2011-10-05 22:41:57
您可以使用std::accumulate。
https://stackoverflow.com/questions/7663062
复制相似问题