Using a for statement to calculate the average of several integers. - C++ Statement

C++ examples for Statement:for

Description

Using a for statement to calculate the average of several integers.

Demo Code

#include <iostream>

int main(int argc, const char *argv[]) {
    int counter = 0;
    int sum = 0;//from   w w w.  j  av  a 2  s  .  c  o m

    std::cout
        << "Enter a list of space separated integers for average calculation"
        << "9999 ends input: " << std::endl;
    std::cin >> sum;

    // set i as input to ensure exit condition
    // increment counter as opposed i
    for (int i = 0; i != 9999; counter++) {
        sum += i;
        std::cin >> i;
    }

    std::cout << "Average: " << sum / counter << std::endl;
    return 0;
}

Result


Related Tutorials