题目:

2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

答案:  书上无答案。

C++技术网辅导详解解答:
    参考代码:

#include <iostream>     
            
			 using namespace std;
int main()
{
    double arr[10] = { 0 };
    int cnt = 0;
    double sum = 0;
    double ave = 0;
    int cnt_over_ave = 0;
    while (cnt < 10) 
    {
        double donation;
        cout << "请输入捐款数额:";
        if (!(cin >> donation))
        {
            cout << "输入不合法,程序即将结束!\n";
            break;
        }
        arr[cnt] = donation;
        sum += donation;
        ave = sum / (cnt+1);
        if (donation > ave)
            cnt_over_ave++;

        cout << "数组所有数字的平均值为:" << ave << ",大于平均值的数的个数为:" << cnt_over_ave<<endl;
        cnt++;
    }
    return 0;
}