平均值

Posted dushenda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了平均值相关的知识,希望对你有一定的参考价值。

问题:

输入一串数字,碰到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;
}

分析:

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

以上是关于平均值的主要内容,如果未能解决你的问题,请参考以下文章