Finding an average without a known number of inputs

Hello,

I am going through a C++ Class and my instructor gave us this assignment without much background or guidance.

I need to allow the user to enter numbers and then determine the high number, low number and then average of them all. The program is designed to break if the user enters a -99. I have seen similar problems on this site but not with an average. My problem is that the min and max are working but the average is coming out wrong. The teach instructed us to use 25, 2.5, 77, 3.5 and 68 which should average to 35.20 but the average is coming up to 42.17 and I don't know why. Please help.

Here is the code as written:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

double number, max, min, average, total=0;
const int exit=-99;
int count;


cout << "Hello welcome to Joe C Smith's Smallest/Largest/Average program\n";
cout << "Please enter a series of numbers; the last number must be -99." << endl;

while (number != exit)
{

cin>> number;

if (count == 0) min = number;
total += number;
count++;

if (number == exit)
break;


else if (number >= max)
{
max >= 0 || max <= 0;
total += number;
max = number;

}
else if (number <= min)
{
min >= 0 || min <= 0;
total += number;
min = number;

}
else
{
max = max;
min = min;
total += number;

}

}
average = total / count;
cout << fixed << showpoint << setprecision(2);
cout << "The smallest number is " << min << "\n";
cout << "The largest number is " << max << "\n";
cout << "The average of all numbers entered is " << average << "\n";
cout << "Have a Great Day!" <<endl;


return 0;
}
Your condition if (number == exit) is in the wrong spot. (It should be the very first thing you check after getting the number with cin>> number;)

Remember, the number of inputs (count) does not include the -99, right?

Also, look at the warnings you get from your compiler and see if you can fix the spots it complains about.

Hope this helps.
It shouldn't count the -99 and I think it is but when I've put it all in Excel and even if I put in -99 in the average it doesn't come up to 42.17.

I tried putting the if (number == exit) first and the if (count...) statement second but then got an error at the first else if statement saying it needed an if statement prior to the else if which didn't make sense to me since it was there

Here is the error I get by moving that statement to right after the cin

[Error] 'else' without a previous 'if'
Last edited on
Well, get rid of the 'else'.

Your original code does count the -99. It (IIRC) just doesn't add it to your total. Essentially, you were computing:

(1 + 2 + 3) / 4

Hope this helps.
I'm not sure what you mean by getting rid of the else. Don't I need it? I also don't understand why I get the error when there is a preceding if statement.

else if (number >= max)
{
max >= 0 || max <= 0;
total += number;
max = number;

}

How do I not count it? Sorry but all of this is still new to me and the teacher isn't very helpful or responsive when emailed.

Anything you can do to help or any guidance you can provide would be appreciated.

Thanks
Last edited on
An 'else' is only necessary if you are handling the fail condition of a previous 'if'. Since there is no 'if' before your 'else' you don't need it. (The one you had you moved, and it uses a 'break' anyway.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  cin >> number;

  if (number == exit)
    break;

  total += number;
  count++;

  if (number > max)
  {
    ...
  }

  ...

Hope this helps.
So the following statement isn't needed as written?

if (count == 0) min = number;
total += number;
count++;

I can take the first line (if statement) out and remove all of the else if statements that follow and make them if statements?

else if (number >= max)
{
max >= 0 || max <= 0;
total += number;
max = number;

}
else if (number <= min)
{
min >= 0 || min <= 0;
total += number;
min = number;

}
else
{
max = max;
min = min;
total += number;

I'll give it a try.

Thanks again for all of your help.

Here is what I tried:

cin>> number;

if (number == exit)
break;

total += number;
count++;


if (number >= max)
{
max >= 0 || max <= 0;
total += number;
max = number;

}
if (number <= min)
{
min >= 0 || min <= 0;
total += number;
min = number;

}
else
{
max = max;
min = min;
total += number;

}

Now it is saying the smallest number entered is 0 (which is incorrect) and the average is now way high.

I am getting a bit confused I guess. The program seems to work as originally written with the exception of the average part of the function and I can't figure out what it is trying to average that it is getting 42.17 as I've tried multiple combinations in excel and don't ever get 42.17.

Pay attention to how many times you are modifying total. (You only need to do it once.)

(Sorry I missed that the first time.)
Last edited on
WOW! That absolutely did the trick. I took out all but the first one. I have no idea why I put all of those in! LOL

I REALLY appreciate all of your help!!!

Thanks!

The final code looks like this for anyone else that may want to know.

Thanks again.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

double number, max, min, average, total=0;
const int exit=-99;
int count;



cout << "Hello welcome to Joe Carman's Smallest/Largest/Average program\n";
cout << "Please enter a series of numbers; the last number must be -99." << endl;

while (number != exit)
{

cin>> number;

if (number == exit)
break;


if (count == 0) min = number;
total += number;
count++;



if (number >= max)
{
max >= 0 || max <= 0;

max = number;

}
if (number <= min)
{
min >= 0 || min <= 0;

min = number;

}
else
{
max = max;
min = min;


}

}
average = total / count;
cout << fixed << showpoint << setprecision(2);
cout << "The smallest number is " << min << "\n";
cout << "The largest number is " << max << "\n";
cout << "The average of all numbers entered is " << average << "\n";
cout << "Have a Great Day!" <<endl;


return 0;
}
Topic archived. No new replies allowed.