-1

UPDATED: I am writing a program that finds the moving average and I cannot seem to figure out why my code does not work. It gets the correct numbers but does not format correctly. For example, my program outputs "34" instead of "34.00" and I thought that making the variable type 'double' would fix this.

Prompt for the program: Write a program that outputs a moving average (see: https://en.wikipedia.org/wiki/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.

When a '0' or a negative number is inputted, the program is supposed to skip that number and reset the count to 0 and continue looping for any positive numbers inputted after that.

Input: 34 99 12 19 44

Expected Output: 34.00 66.50 48.33 41.00 41.60

My Output: 34 66.5 48.3333 41 41.6

My Code:

#include <iostream>
using std::cin; using std::cout; using std::endl;

double input;
double count = 0;
double sum;
double avg;
double prev = 0;

int main(){
  while (cin >> input){
    if (input == 0 || input < 0)
      count = 0;
    else
      count++;

    sum = input + prev;
    prev = sum;
    avg = sum / count;
    cout << avg << ' ';
  }
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 4
    put whatever you want in the while loop in braces `{}` – Tyler Jan 30 '20 at 18:42
  • Thank you! I will try this and edit my question if it does not work. – CodeGuy7153 Jan 30 '20 at 18:48
  • [Useful reading on statements in C++](https://en.cppreference.com/w/cpp/language/statements). – user4581301 Jan 30 '20 at 18:50
  • 1
    Note: You are not summing all of the inputs. `sum = input + prev;` `sum` will only be the sum of current number and the last number. That said, it's not what you want to do. I recommend leaving off with the code for a while and working this out on paper. – user4581301 Jan 30 '20 at 18:54
  • 1
    Just a tip. You can change your if condition to `input <= 0`. – Emmanuel Mathi-Amorim Jan 30 '20 at 18:54
  • 1
    Read [*How to debug small programs*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Basile Starynkevitch Jan 30 '20 at 19:00
  • Have you tried `prev = sum;` instead of `prev = input;`? – François Andrieux Jan 30 '20 at 19:01
  • 1
    When you clear `count` with `count = 0;` you probably want to make it `1` instead (or you will divide by zero). You'll also probably want to reset `prev` to zero. – François Andrieux Jan 30 '20 at 19:02
  • @FrançoisAndrieux I just tried this and I got the correct numbers but the numbers don't go to the hundredths place. I will update the question. – CodeGuy7153 Jan 30 '20 at 19:05
  • @CodeGuy7153 Then use `setprecision()` – Sandra K Jan 30 '20 at 19:06
  • @SandraK is there a way to include "0" as well because when I try this my program prints out "66.5" instead of "66.50." – CodeGuy7153 Jan 30 '20 at 19:12
  • Get rid of `prev`. Use `sum += input;` – Thomas Matthews Jan 30 '20 at 19:21
  • @FrançoisAndrieux my question was not about the correct number of decimal points, this came up after some adjustments to my code as a result of others suggesting ideas. – CodeGuy7153 Jan 30 '20 at 19:42
  • @CodeGuy7153 As the question is now, it is a duplicate. But in the future, you should not edit the question to implement recommended changes. Edits should leave the core question unchanged. Otherwise it changes the context in which comments and answers are written and invalidates them. Questions need to keep making sense with regards to the comments and answers so that it is useful for future readers. – François Andrieux Jan 30 '20 at 19:45

1 Answers1

0

You are complicating things, you dont need all that arithmetic:

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;
     }    
  }
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53