Thread: New assignment.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    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;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Where I am stuck is the "Range" ordeal.

    Well, what sort of 'ordeal' are you having with it, exactly?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Can you give me an example of this in code?
    I really don't know where to begin with all the set / min/max?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    What part doesn't make sense to you? Post an attempt to get it working, and then go from there.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    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.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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?

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM