Ask Question
30 July, 00:41

Write a program that outputs a moving average. For a series of whitespace delimited integers (note, the running average should be rounded to the hundredth place. However, if a non-positive number is encountered, the average should be reset and a newline character emitted. Otherwise, the running averages should be separated by a space.

+2
Answers (1)
  1. 30 July, 01:49
    0
    using std::cin; using std::cout; using std::endl;

    double input;

    double count = 0;

    double sum;

    int main () {

    while (cin >> input) {

    if (input > 0) {

    count++;

    sum + = input;

    cout << (sum / count) << " ";

    }

    else{

    count = sum = 0;

    }

    }

    }

    Explanation:

    This computes the moving average as you desire in C++. The else{} block resets the sum and count to 0 if a non-positive number is encountered.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that outputs a moving average. For a series of whitespace delimited integers (note, the running average should be rounded ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers