平均值

问题:

输入一串数字,碰到0停止循环计算平均值。

代码:

#include<iostream>

using namespace std;

int main(){
    int i = 1;
    double a{ 0 }, sum{ 0 };
    for (;;){
        i++;
        cout << "input a set of numbers";
        cin >> a;
        sum += a;
        if (a == 0){
            break;
        }
    }
    cout << "the average is " << (sum / i);
    return 0;
}

分析:

常量应该在循环外面定义。

猜你喜欢

转载自www.cnblogs.com/dushenda/p/9651466.html
今日推荐