Thread: help with c++

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    30

    help with c++

    hello all, i am new to this site and am hoping to find some help here.

    i am required to write a program with these specifications:

    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.

    so far, i have this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	double num1 = EOF;
    
    	while (num1 >= 0 && num1 <= 100)
    	{cin >> num1;
    	if (num1 == EOF) break;
    	}
    
    
    }
    i am new to this stuff, and damn it is hard. i dont know if i am on the right track with this, but i am not familiar with using ctrl+z to end the program. any help would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    while ( cin >> num )

    Will loop until you hit ctrl-z (or enter something which isn't a number)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Programming is about partitioning problems. In this case, you can divide the assignment into four parts.

    1) Keep reading numbers until you reach end-of-file (achieved on the Win32 console by pressing Ctrl+Z). For this, you need a loop that runs while you can still read numbers. Tip: "while you can still read numbers" can be expressed as
    Code:
    double num;
    while(cin >> num) {
    }
    This snippet is somewhat tricky, which is why I'm giving it to you. It does the following:
    a) At the start of every loop iteration, it reads a number - that's the cin >> num part.
    b) The cin >> num part not only fills num with the number read, but it also can be used like cin alone. (That's why you can chain input statements, like cin >> num1 >> num2 - here you use (cin >> num1) like cin itself, making this equivalent to cin >> num1; cin >> num2;.)
    c) cin in the condition of an if or while means "is cin still usable?" cin is usable as long as it succeeds in reading numbers; it becomes unusable when it fails to read one. So while(cin >> num) effectively means, "loop as long as you succeed in reading numbers". Hey, that's exactly what we started with!
    2) Ignore all numbers outside the range [0,100]. For this you need a single if inside the loop.
    3) Calculate the mean of all numbers entered.
    4) Calculate the minimum and maximum of all numbers entered.

    Since you presumably don't know about vectors yet (and also for efficiency reasons) you want to achieve parts 3 and 4 without saving every single number entered. So you need to come up with a way to do these one number at a time. (Hint: how would you calculate the mean of a set of numbers? The minimum and maximum?)

    Commenting on what you have so far:
    1) EOF has no place in this program. It's something that belongs with C-style I/O, which you're not using.
    2) You break the loop if the number is outside the range. Not good, since you're supposed to ignore them, not stop the program.
    3) You test the number before ever reading a value.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you need a few things in your program first of all.

    1. a way to keep track of the largest and smallest numbers entered.
    2. a way to keep track of how many numbers were entered.
    3. a way to check to see if the user pressed control-z

    3 also depends on what platform for which you're writing code. Windows/DOS and UNIX/Linux handle control-z completely differently.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Since you don't know how many numbers the user will enter, you need a data structure that grows rather than a plain old array, so std::vector<double> seems like a good idea. If you're not familiar with it, do some research about vectors, because it's extremely useful. You might also want to have a look at some of the algorithms in the <algorithm> header.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elkvis View Post
    3. a way to check to see if the user pressed control-z

    3 also depends on what platform for which you're writing code. Windows/DOS and UNIX/Linux handle control-z completely differently.
    Under UNIX, Ctrl+Z really isn't meaningful. I think it's safe to assume that the instruction mean "hit EOF" and go from there.

    Quote Originally Posted by cpjust View Post
    Since you don't know how many numbers the user will enter, you need a data structure that grows rather than a plain old array,
    You don't need either, for calculating the mean, minimum and maximum.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    Quote Originally Posted by CornedBee View Post
    Programming is about partitioning problems. In this case, you can divide the assignment into four parts.

    1) Keep reading numbers until you reach end-of-file (achieved on the Win32 console by pressing Ctrl+Z). For this, you need a loop that runs while you can still read numbers. Tip: "while you can still read numbers" can be expressed as
    Code:
    double num;
    while(cin >> num) {
    }
    This snippet is somewhat tricky, which is why I'm giving it to you. It does the following:
    a) At the start of every loop iteration, it reads a number - that's the cin >> num part.
    b) The cin >> num part not only fills num with the number read, but it also can be used like cin alone. (That's why you can chain input statements, like cin >> num1 >> num2 - here you use (cin >> num1) like cin itself, making this equivalent to cin >> num1; cin >> num2;.)
    c) cin in the condition of an if or while means "is cin still usable?" cin is usable as long as it succeeds in reading numbers; it becomes unusable when it fails to read one. So while(cin >> num) effectively means, "loop as long as you succeed in reading numbers". Hey, that's exactly what we started with!
    2) Ignore all numbers outside the range [0,100]. For this you need a single if inside the loop.
    3) Calculate the mean of all numbers entered.
    4) Calculate the minimum and maximum of all numbers entered.

    Since you presumably don't know about vectors yet (and also for efficiency reasons) you want to achieve parts 3 and 4 without saving every single number entered. So you need to come up with a way to do these one number at a time. (Hint: how would you calculate the mean of a set of numbers? The minimum and maximum?)

    Commenting on what you have so far:
    1) EOF has no place in this program. It's something that belongs with C-style I/O, which you're not using.
    2) You break the loop if the number is outside the range. Not good, since you're supposed to ignore them, not stop the program.
    3) You test the number before ever reading a value.
    thank you, this is exactly what i needed. i just have one more quick question:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double num, total = 0;
    
    	while (cin >> num)
    	{
    		if (num>100||num<0)
    		{
    		cout << "Out of range. Ignored." << endl;
    		}
    		else
    		cin >> num;
    	}
    		
    	total = num/num;
    	cout << "the total is " << total << endl;
    }
    it all compiles correctly and the numbers work, with the ones outside the range displaying the message. but how do i figure out the average and range? if i dont know how many numbers they are going to be putting in, how many do i divide by for the average? similarly, how do i know which numbers are going to be the max and min for the range? right now the total is displaying 1, and it is obvious why, but i am not sure how to get the right denominator in the formula.

    thanks again for your continued help.

    any more help is appreciated.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    actually, while i am looking through. this one was giving me problems as well.

    Code:
    Write a program that reads two integers num1 and num2 from the user, then displays the total of the all
    of the numbers between num1 and num2 (inclusive). Your program should handle the following:
    1. Make sure the numbers entered are between 0 and 100.
    2. Make sure the second number is greater than or equal to the first number entered.
    */
    
    #include <iostream>
     using namespace std;
    
     int main()
     {
        int num1, num2, num3, i=1;
            cout << "Please enter an integer between 0 and 100: " << endl;
            cin >> num1;
    		long total_total = 0;
            if (num1 >= 0 && num1 <= 100)
            {           
            cout << "Please enter an integer between " << num1 << " and 100: " << endl;
            cin >> num2;
    		num3 = num1;
    		if (num2 >= num1 && num2 <= 100)
                    {
    					while (num1 < num2) 
                        {
                   
    				total_total = total_total + num1;
    				num1++;
    				
                                  
                        }
                        cout << "The total of the integers between " << num3 << " and " << num2 << " is: " << total_total << endl;
                    }
                    else
                    {
                        cout << "Please read and follow the directions!" << endl;
                    }
            }
            else
            {
                cout << "Please read and follow the directions!" << endl;
            }
     }
    everything is working correctly, but the program isnt adding the numbers correctly. It is supposed to add all the integers between the 2 numbers selected. I am not exactly sure what it is doing, but it isnt adding the numbers correctly.

    this one is actually due this coming morning, and ive been killing myself over it for the past few days.

    thanks again guys. it is appreciated.

  9. #9
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    If I understand the problem correctly, all you have to do is change
    Code:
    while(num1 < num2)
    to
    Code:
    while(num1 <= num2)
    The reason being the problem says it's inclusive, so you must include all numbers in the range, including the endpoints.
    Last edited by Sentral; 02-24-2009 at 08:49 PM.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by kbpsu View Post
    but how do i figure out the average and range?
    http://en.wikipedia.org/wiki/Moving_average


    if you need help deciphering the notation let me know. you can figure it out pretty easily from the definition of mean.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    Quote Originally Posted by Sentral View Post
    If I understand the problem correctly, all you have to do is change
    Code:
    while(num1 < num2)
    to
    Code:
    while(num1 <= num2)
    The reason being the problem says it's inclusive, so you must include all numbers in the range, including the endpoints.
    that was the problem. woulda saved me a lotta headache if i would have just added that = sign.

    thanks a lot.

  12. #12
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by kbpsu View Post
    that was the problem. woulda saved me a lotta headache if i would have just added that = sign.

    thanks a lot.
    It happens. The little mistakes are sometimes the biggest problems.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    Quote Originally Posted by m37h0d View Post
    http://en.wikipedia.org/wiki/Moving_average


    if you need help deciphering the notation let me know. you can figure it out pretty easily from the definition of mean.
    i know how to figure out the average and range if the numbers are known. but for this problem, i am not sure how many numbers the user is going to input, so i dont know what number to divide by. and for the range, i dont know how to get the program to recognize which number was the max/min.

  14. #14
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by kbpsu View Post
    i know how to figure out the average and range if the numbers are known. but for this problem, i am not sure how many numbers the user is going to input, so i dont know what number to divide by. and for the range, i dont know how to get the program to recognize which number was the max/min.
    In your loop just use a counter variable that increments each time the loop is cycled. This is the amount of numbers the user inputs.

    For the min/max, a simple algorithm is to have variables min/max initialized to zero. Then in your loop just compare the current input with the max or the min using an if statement. If the current input is larger or smaller, assign the current input into the respective min/max variable.

    *OH NOES 666 posts!
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    You don't need either, for calculating the mean, minimum and maximum.
    Oh, that's right. I got it confused with Median.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed