I'm have serious trouble with this program. The average and the range do not work. The program runs...I just don't know how to fix this. Please help.

I have to find the average and the range of an unlimited number of integers between 0 and 100.

#include <iostream>
using namespace std;

int main ()
{
	double num, sum=0, min1=0, max1=0, range, count=0;

	cin >> num;
	
	while (cin >> num)
	{
		++count;
		if (num < 0 || num > 100)
		{	
			cout << "out of range, value ignored" << endl;
		}
	
		else
		{
			for (min1 = num; num > 0; )
			{
				if (num < min1) min1 = num;
				{
					cin >> num;
				}
			}
			for (max1 = num; num > 0; )
			{
				if (num > max1) max1 = num;
				{
					cin >> num;
				}
			}
		
		sum += num;
		range = max1 - min1;
		}
	}
	cout << "The average is " << sum / count << endl;
	cout << "The range is " << range << endl;
}
}

Recommended Answers

All 9 Replies

Average: sum of all the numbers entered divided by the quantity of numbers entered. So if I enter 5 numbers (1, 2, 3, 4 and 5) the average will be (1+2+3+4+5)/5. You need to do the same thing in your program. Just keep summing them up as you enter the numbers, keeping track of how many you entered. When all done entering the numbers then you can make the final calculation.

The range is found by keeping track of the largest value entered and the smallest value entered -- you need two variables to do that. When you enter a number check to see if it is smaller than the smallest value. If yes, then change the smallest value to be the same as the number just entered. Then check if the number just entered is greater than the largest number entered so far. If yes, then change the value of the greatest number to be the value of the number just entered. When you stop entering numbers you don't have to do anything more because its all done.

Average: sum of all the numbers entered divided by the quantity of numbers entered. So if I enter 5 numbers (1, 2, 3, 4 and 5) the average will be (1+2+3+4+5)/5. You need to do the same thing in your program. Just keep summing them up as you enter the numbers, keeping track of how many you entered. When all done entering the numbers then you can make the final calculation.

The range is found by keeping track of the largest value entered and the smallest value entered -- you need two variables to do that. When you enter a number check to see if it is smaller than the smallest value. If yes, then change the smallest value to be the same as the number just entered. Then check if the number just entered is greater than the largest number entered so far. If yes, then change the value of the greatest number to be the value of the number just entered. When you stop entering numbers you don't have to do anything more because its all done.

I understand how to find the mean and range...to me the program looks like it should work? I'm not understanding what went wrong?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	double num, sum=0, min1=0, max1=0, range, count=0;

	cin >> num;
	
	while (cin >> num)
	{
		++count;
		sum += num;
		if (num < 0 || num > 100)
		{	
			cout << "out of range, value ignored" << endl;
		}
	
		else
		{
			for (min1 = num; num > 0; )
			{
				if (num < min1) min1 = num;
				{
					cin >> num;
				}
			}
			for (max1 = num; num >0; )
			{
				if (num > max1) max1 = num;
				{
					cin >> num;
				}
			}
		}
		range = max1 - min1;
	}
	cout << "The average is " << sum / count << endl;
	cout << "The range is " << range << endl;
}

Do I need the #include string? I have to have this done tonight ;/

Here much easier... =) This finds the sum of all the values between the range of values you enter.

#include <iostream>

using namespace std;

int main()

{
	long Sum = 0;
	long x, y;
	
	cout << "Enter the first #: ";
	cin >> x;
	cin.ignore();
	cout << "Enter the last #:  ";
	cin >> y;
    cin.ignore();
	// If the last number > then the first, inverse them

	if( x > y )
	{
		int Z;
		
		Z  = x;
		x = y;
		y  = Z;
	}
	
	for( int Counter = x; Counter <= y; Counter++ )
		Sum += Counter;

	cout << "Sum of numbers from " << x << " to " << y << " = "
         << Sum << endl;
    cout << "Press Enter to exit";
    cin.get();    
    return 0;
}

Here much easier... =) This finds the sum of all the values between the range of values you enter.

#include <iostream>

using namespace std;

int main()

{
	long Sum = 0;
	long x, y;
	
	cout << "Enter the first #: ";
	cin >> x;
	cin.ignore();
	cout << "Enter the last #:  ";
	cin >> y;
    cin.ignore();
	// If the last number > then the first, inverse them

	if( x > y )
	{
		int Z;
		
		Z  = x;
		x = y;
		y  = Z;
	}
	
	for( int Counter = x; Counter <= y; Counter++ )
		Sum += Counter;

	cout << "Sum of numbers from " << x << " to " << y << " = "
         << Sum << endl;
    cout << "Press Enter to exit";
    cin.get();    
    return 0;
}

I don't think you read what I'm trying to do. I need to find the average and range of the numbers. I also need to be able to put in as many numbers as I want.

when is wrong is that you have too many cin's. YOu are making this a lot more difficult than it really is. All you need is one cin loop. You don't need any for loops in this program.

while( cin >> num)
{
   // do all calculations here
}
// now find the mean value and print everything.

BTW: In case you don't know, you have to press Ctrl+Z <Enter> to exit that loop.

when is wrong is that you have too many cin's. YOu are making this a lot more difficult than it really is. All you need is one cin loop. You don't need any for loops in this program.

while( cin >> num)
{
   // do all calculations here
}
// now find the mean value and print everything.

BTW: In case you don't know, you have to press Ctrl+Z <Enter> to exit that loop.

So your saying I don't need all those brackets? Like this? I think the mean is working now...but there is a warning in the program and the range is still not working.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	double num, sum=0, min1=0, max1=0, range, count=0;

	cin >> num;
	
	while (cin >> num)
	{
		++count;
		sum += num;
		if (num < 0 || num > 100)
			cin.ignore();
		continue;
			
		
			
			cout << "out of range, value ignored" << endl;
		
	
		
		
			for (min1 = num; num > 0; )
			
				if (num < min1) min1 = num;
		
			
			for (max1 = num; num > 0; )
			
				if (num > max1) max1 = num;
				
			
		
		range = (max1 - min1);
	
	}
	double mean = (sum/count);
	cout << "The average is " << mean << endl;
	cout << "The range is " << range << endl;
}

Still not solved. Anyone have any suggestions?

I am saying you do not need any for loops -- NONE. Just delete lines 26 thru 33 because that is the wrong way to do it. Here is all you have to do. As I said before, you are overthinking the problem, making the program too complicated.

top of loop
   enter a number
   is number < lowest
      yes, set lowest = number
   if number > highest
      yes, set highest = number
   add number to sum accumulator
   increment quantity if numbers entered
end of loop
calculate mean
commented: Great Advice +1

Ok. I started from scratch and I got it working. Thanks for the advice. You really helped me think about how the problem should be solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.