到目前为止,我试图找到一种方法来获得这个数组的平均值的尝试都没有结果。任何帮助,将不胜感激。
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main( )
{
const int MAX = 100;
double voltages[MAX];
double average;
ifstream thefile("c:\\voltages.txt");
if(!thefile)
{
cout<<"Error opening file"<<endl;
system("pause");
exit(1);
}
for(int count = 0; count < MAX; count++)
{
thefile >> voltages[count];
cout << voltages[count++] << endl;
average = voltages[count++]/count;
if(count == 0 || thefile.eof())
{
break;
}
}
cout << average;
cout << "\n";
system("PAUSE");
return 0;
}
电压文件是
100.8
120.4
121.4
111.9
123.4
但最多可以有 100 个双打。