编写一个程序,最多将10个donation值读入到一个double数组中。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
double donation[10]; //创建double数组
int len, i, j;
double sum, average, num;
string line;
sum = num = 0.0;
cout << "Enter 10 donation!" << endl;
for (i = 0; i < 10; i++)
{
getline(cin, line, '\n');
len = line.size();
for (j = 0; j < len; j++)
{
//line[j]是否是数字
if (isdigit(line[j]) == false)
{
cout << "not digit!" << endl;
break;
}
}
donation[i] = atof(line.c_str()); //将AscII字符串转换为单精度浮点数
sum += donation[i];
}
average = sum / i;
for (i = 0; i < 10; i++)
{
if (donation[i] > average)
{
num++;
}
}
cout << "The average = " << average << endl;
cout << "In this array has " << num << " large than avearge" << endl;