Thread: New assignment.

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You really need to be consistent with indenting and spacing. Also, as a newcomer to the language, it's a good idea to put bracket around *every* condition. Here is a cleaned-up version of the code with comments on the remaining problems:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double 
    		n, 
    		count=0, 
    		sum=0, 
    		min=0, // No, set high. 
    		max=100; // No, set low.
    	for (;;)
    	{
    		if (cin.eof()) 
    		{
    			break;
    		}	
    		while (cin >> n)
    		{
    			if (n < min) 
    			{
    				min = n; 
    			}
    		/*
    			This is wrong - doesn't depend on previous comparison.	
    		*/
    			else if (n > max)  
    			{
    				max = n;
    			}
    			if (n < 0 || n > 100)
    			{
    				cout << "out of range; ignored." << endl;
    			}
    			else 
    			{
    				++count;
    				sum += n;
    			/*
    				This is wrong - calculation needed outside of loop.
    				Also, 'range' is undeclared.
    			*/				
    				range = max-min;
    			}
    		}	
    	}
    /*
    	Note: cast is unnecessary.
    */
    	cout << "The average is " << float (sum)/count << endl;
    	cout << "The range is " << range << endl;
    }

  2. #17
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Ok, how does this look:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double n, 	count=0, 	sum=0, min=100, max=0, range=0;
    	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;
    }

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You should still move the range calculation outside of the loop, and get rid of the cast in the printout.

  4. #19
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Ok, move the range calculation out of loop, See below, am I right? Also, what do you mean "cast"

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double n, 	count=0, 	sum=0, min=100, max=0, range=0;
    	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;
    }

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Ok, move the range calculation out of loop

    Yes, out of the loop and far to the right. Is it really so hard for you to indent properly?

    >> Also, what do you mean "cast"

    As in "conversion to some other type". You are "casting" 'sum' to a float, which is unnecessary.

    Otherwise, everything looks okay.

  6. #21
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    I am sorry about the indenting. WHen I paste, it, somehow it changes.

    Ok. I am going to repost. can you try and compile it for me and let me know if it works?

    I can't compile on the computer I am using right now. thank you soo very much

  7. #22
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    here goes:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double n, 	count=0, 	sum=0, min=100, max=0, range=0;
    	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 " <<  (sum)/count << endl;
    cout << "The range is " << range << endl;
    }

  8. #23
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by Titanguy View Post
    "Also, in your original code, count should be an integer.
    In C/C++, floating point values (floats and doubles) are designed for numbers which can have decimal places in them. Integer types, on the other hand, are whole numbers that have no fractional or decimal parts to them.

    When counting things, it usually makes sense to use an integer type. In your example, you can have 5, 10, 12, 28, or whatever numbers input, but you can't have 3.1, 8.6 or 9.2 -- you read numbers one at a time and can't read less than one, so you'll never have anything after the decimal point. So it makes more sense to count the numbers you've read using an integer type.

    So in your code, use :

    int count = 0;

    Instead of making it a double.

    It makes no difference for the stuff you're doing now, but you'll eventually hit cases where it does. Might as well get in the habit of doing it correctly now.

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