I have another question for my homework. Here is the problem:
Keep inputting numbers from the user until the user enters a 0. After each number, print a "running average". That is, print the average of the last three numbers entered. Be careful how you handle the first two numbers entered, before there are three numbers for the running average. (I'll accept any reasonable solution to handling the first two numbers).


So far, I have:

#include "stub.h"
int main()
{
   int x;
   int sum = 0;
   int num1;
   int num2;
   int num3;
   
 do
 {
       cin >> x;
       
       sum = sum + x;
       counter = counter + 1;
   
       num1 = x;
       num2 = num1 + x;
       num3 = num1 + num2 + x;
   
       cout << num3/3 << endl;
   
}
   
   while (x!=0); 
   
   if(x==0)
   return 0;
   
   cin.get();
       
    getchar();
	return 0;
}

This isn't working for me, and I'm not sure how to fix it or how to get the average of only the last three inputted numbers.

(I'll accept any reasonable solution to handling the first two numbers)

So nice of you :icon_wink:

This isn't working for me, and I'm not sure how to fix it or how to get the average of only the last three inputted numbers.

Is there something about explaining why you think it doesn't work that is just lost on new posters? If you were going to help someone, wouldn't you want to know what's wrong? :icon_rolleyes:

There's an obvious error in this code. Let's assume x=3:

num1 = x;        // num1 now = 3
       num2 = num1 + x;   // num2 now = 6 (3+3)
       num3 = num1 + num2 + x;    // num3 now = 12 (3+6+3)
   
       cout << num3/3 << endl;

How is this averaging anything? Think through how to average 3 numbers again. Maybe do it on paper to get the process figured out step by step. Then translate those steps into code.

I have another question for my homework. Here is the problem:
Keep inputting numbers from the user until the user enters a 0. After each number, print a "running average". That is, print the average of the last three numbers entered. Be careful how you handle the first two numbers entered, before there are three numbers for the running average. (I'll accept any reasonable solution to handling the first two numbers).


So far, I have:

#include "stub.h"
int main()
{
   int x;
   int sum = 0;
   int num1;
   int num2;
   int num3;
   
 do
 {
       cin >> x;
       
       sum = sum + x;
       counter = counter + 1;
   
       num1 = x;
       num2 = num1 + x;
       num3 = num1 + num2 + x;
   
       cout << num3/3 << endl;
   
}
   
   while (x!=0); 
   
   if(x==0)
   return 0;
   
   cin.get();
       
    getchar();
	return 0;
}

This isn't working for me, and I'm not sure how to fix it or how to get the average of only the last three inputted numbers.

#include "stub.h"
int main()
{
   int x;
   int sum = 0;
   int num1=1;//Just init previous 3 numbers
   int num2=2;
   int num3=3;
   
 do
 {
       cin >> x;
       
       sum = num1 + num2 + x;

       num1 = num2;
       num2 = x;
   
       cout << double(sum)/3. << endl;
   
}
   
   while (x!=0); 
   
   if(x==0)
   return 0;
   
   cin.get();
       
    getchar();
	return 0;
}

Think this is what you want.

Comments
Giving a solution while others are trying to help

zhelih
First off, don't just give people code. The whole point of homework is so that the student has to use his brain.

Second off, it doesn't work, because the user can't input the first three numbers properly and it doesn't calculate the average.

sfurlow2
Remember, the average is (sum of numbers entered) / (count of numbers entered).
In C and C++, remember that / is integer division on integers, and floating point division on floats. So int / int is an int. Whereas float / int is a float. You can fix it by casting:

int sum;
int count;
float average;
...
average = float( sum ) / count;

For handling the special cases, consider them separately:

one number entered
the average of one number is itself. (x / 1 = x)

two numbers entered
the average of two numbers is the sum over two. (x + y) / 2

three or more numbers entered
the average is the sum of all the numbers entered over the number of numbers entered:
(x + y + z) / 3
(w + x + y + z) / 4
(v + w + x + y + z) / 4
etc.

Looking at this, you'll recognize that "two numbers entered" is not a special case at all...

In fact, one number entered isn't either.

So you only really need three things:

  1. the sum of the numbers entered
  2. the number (or count) of numbers entered
  3. the value of the last number entered

Think about it a bit and you'll get it just fine. If you still have trouble, post back with what you did and we'll help some more.

This question has already been answered. Start a new discussion instead.