# New assignment.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-18-2009
Titanguy
New assignment.
Please help, running out of time. Assignment is attached. What I have so far is below. Where I am stuck is the "Range" ordeal. Please help:

Code:

``` #include <iostream> using namespace std; //Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the //range. //Notes: //• You do not know ahead of time how many numbers will be in the list. //• When you want to stop entering numbers, enter control-z. //• The range is the difference between the lowest and the highest number. //• The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range. int main() { double n, count=0, sum=0; for (;;) {         if( cin.eof( ) ) break;         while (cin >> n)                 if (n <0 || n >100){                         cout << "out of range; ignored." << endl;                 }                 else {                         ++count;                         sum += n;                 } } cout << "The average is " << float (sum)/count << endl; }```
• 10-18-2009
tabstop
To find the difference between the lowest and the highest number, you must know what they are. If I give you ten numbers, one at a time, and then at the end ask you for the lowest and the highest, what are you going to do to answer my question?
• 10-18-2009
Sebastiani
>> Where I am stuck is the "Range" ordeal.

Well, what sort of 'ordeal' are you having with it, exactly?
• 10-18-2009
Titanguy
I know the program I posted works perfectly. The problem I am having is adding the fact of:

"The range is the difference between the lowest and the highest number"

I have to add this programming to my program, and I am really lost with the xmin an xmax stuff?

What it is asking me to do is have the range at bottom under the average:

cout << "The range is " << range ;

The "range" is the difference from the lowest and highest numbers entered. So if you enter 50 different numbers, you need to auto calculate the lowest number entered, to the highest number entered. Max# - Min# = Range.

Need help adding that to my program.
• 10-18-2009
Sebastiani
Set 'min' to the lowest possible value. Set 'max' to the highest possible value. On each iteration of the loop, compare these with the input and reassign them if they are exceeded. After the loop, subtract and print.

For obtaining the min/max possible value of a number you can std::numeric_limits (header: <limits>), eg: std::numeric_limits< double >::min( ), etc.
• 10-18-2009
Titanguy
Can you give me an example of this in code?
I really don't know where to begin with all the set / min/max?
• 10-18-2009
Sebastiani
What part doesn't make sense to you? Post an attempt to get it working, and then go from there.
• 10-19-2009
Titanguy
I am unsure how to use the set command with the min/max, and unsure how to implement it so it only shows the difference of the lowest and highest numbers entered. Did you see the attachment on my first post? It explains what I am talking about. the assignment is due today and I am soooo struggling.

for example:

if you enter:

1.11
2.22
3.33
4.44
5.55
^z
The average is 3.33 (I am good up to this point)
The range is 4.44 (this is where I am stuck)

To Calculate the range, it is using the highest number entered 5.55 and subtracting the lowest number entered 1.11 = 4.44, but it has to know and only use the min and max numbers no matter what order, for example:

4.44
2.22
1.11
3.33
5.55

It would still only use 1.11 as min and 5.55 as max.

Can you show me an example of code to do this? (Actual code) or help me implement this into my existing code?

I would sincerely appreciate it.
• 10-19-2009
Sebastiani
Again, you need to at least make an attempt to do this yourself. Write some code, post it here if you run into any problems, and we'll be glad to point you in the right direction.
• 10-19-2009
KCfromNC
Outside the loop create variables min and max. Set min to 100 and max to 0 since this is the range of input you allow.

Inside the loop, for each input, if the input is greater than max set max to the number. This way, when you're done with the loop max will hold the biggest number you've seen so far.

Likewise, if the input is less than min, set min to that input.

After the loop, print min subtracted from max.

Also, in your original code, count should be an integer. There's also no point in casting sum to a float in the output line.
• 10-19-2009
Titanguy
thank you for your help.
so, in my first line I should:

double n, count=0, sum=0, min=100, max=0;

(Is this right?)

Then with each loop do:

if (n > max) max=n
if (n < min) min = n

Is this right?

Also, what do ya mean about:

"Also, in your original code, count should be an integer. There's also no point in casting sum to a float in the output line. "

I am very new to C++, no experience at all.
• 10-19-2009
Sebastiani
Yep, that look about right. You should post the entire program, as well (be sure to use code tags and indent your code). Oh, and I was actually backwards about the initialization, eg. min should be set high and max set low, but anyway, it looks like you've got that straight.
• 10-19-2009
Titanguy
Or do I do:

if (n<min) min = n;
else if (n>max) max = n;

And if so, where would I place this in my code?
• 10-19-2009
Sebastiani
>> Or do I do:

No. They should be treated separately.

>> And if so, where would I place this in my code?

Inside the loop, of course.
• 10-19-2009
Titanguy
Ok, so will this work? (don't have a compiler on this computer)
Code:

```#include <iostream> using namespace std; int main() { double n, count=0, sum=0, min=0, max=100; for (;;) {         if( cin.eof( ) ) break;         while (cin >> n)                                 if (n<min) min = n;                                 if (n>max) max = n;                   if (n <0 || n >100){                         cout << "out of range; ignored." << endl;                 }                 else {                         ++count;                         sum += n;                                                 range= max-min                 } } cout << "The average is " << float (sum)/count << endl; cout << "The range is " << range << endl; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last