求平均数
小弟写了个求平均数的程序,让录入10个成绩,但是可以提前结束录入,然后算平均数,打印到屏幕.但是求平均数的结果是错误的,请哪位指点一下,谢谢!#include <iostream>
using namespace std;
const int Lim=10;
void Input(double arr[Lim]);
void Average(double array[]);
void Show(double array[]);
int main()
{
double arrDou[Lim];
Input(arrDou);
Show(arrDou);
Average(arrDou);
return 0;
}
void Input(double arr[Lim])
{
int flag;
double temp;
for(int i=0; i<Lim; i++)
{
cout << "Enter the scores#" << (i+1) << ":";
cin >>temp;
if(!cin)
{
cin.clear();
while(cin.get() != '\n')
continue;
cout << "Bad input;Input process terminated.\n";
break;
}
else if(temp < 0)
break;
arr[i]=temp;
}
for(;i<Lim; i++)
arr[i]=0;
}
void Average(double array[])
{
unsigned long aver, sum;
int i;
for(i=0; i<Lim; i++)
{
sum+=array[i];
aver=sum/Lim;
}
cout << "Average: " << aver << endl;
}
void Show(double array[])
{
int i;
for(i=0; i<Lim; i++)
cout << (i+1) << ":" << array[i] << "\n";
}