Thread: can i do this some other way?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    can i do this some other way?

    Some info, im trying to find a users jacket size depending on their heigh, weight and age, the algorithm is simple.

    (height * weight)/ 288.

    But here is the tricky part, if the person is above age 30 then the jacket size should be adjusted by 0.125 for every 10 years. So if the user is 35, there will be no adjustments, but if the user is 40 then 0.125 will be added to the jacket size if the user is 50 then 0.125 + 0.125=0.250 will be add to the jacket size and so on.

    my function does that, but i feel that im making complicated calculations and that there must be some easier way or cleaner way to do it.

    Code:
    //function to compute jacket size
    	double jacketsize(double height, double weight, int age)
    	{
    		double jacket=0;
    		float adjust =0.125;
    		int count;
    
    	if (age <40)
    
    			jacket= (height * weight) /288;	
    
    	else if (age>=40)
    		{
    			count  = (age - 30)/10;			 
    			jacket= ((height * weight) /288) + (count * adjust);	
    		}
    		return jacket;
    
    	}
    When no one helps you out. Call google();

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you could write it this way:
    Code:
    //function to compute jacket size
    double jacketsize(double height, double weight, int age)
    {
    	double adjust = 0.125;
    	double jacket = (height * weight) / 288.0;
    	if (age >= 40)
    		jacket += static_cast<double>((age - 30) / 10) * adjust;
    	return jacket;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You really don't need the if-then-else. Just use one calculation for both, the one in the else:
    Code:
    	int count  = (age - 30)/10;			 
    	jacket= ((height * weight) /288) + (count * adjust);
    Or if you prefer:
    Code:
    	jacket= ((height * weight) /288) + (((age - 30)/10) * adjust);
    Last edited by swoopy; 03-07-2006 at 10:06 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    swoopy: what happens to your algorithm is age = 29? count will turn negative and the adjustments will be incorrect.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by swoopy
    You really don't need the if-then-else. Just use one calculation for both, the one in the else:
    Code:
    	int count  = (age - 30)/10;			 
    	jacket= ((height * weight) /288) + (count * adjust);
    Or if you prefer:
    Code:
    	jacket= ((height * weight) /288) + (((age - 30)/10) * adjust);
    He does need the if statement - try entering a 10 year old into your calculation, you will see the result is different.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ancient Dragon and Bench, good point.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You might want to make the adjust variable a static constant
    Code:
    static const double adjust = 0.125;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Quote Originally Posted by laserlight
    Perhaps you could write it this way:
    Code:
    //function to compute jacket size
    double jacketsize(double height, double weight, int age)
    {
    	double adjust = 0.125;
    	double jacket = (height * weight) / 288.0;
    	if (age >= 40)
    		jacket += static_cast<double>((age - 30) / 10) * adjust;
    	return jacket;
    }
    thats basically doing the same calculations tho, i mean you are still subtracting to find out how many times you have to adjust.

    i was thinking a way along the lines of not using the subtraction.
    When no one helps you out. Call google();

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i was thinking a way along the lines of not using the subtraction.
    If you dont want to use a substraction, then go around checking with if/else, mapping ranges to values. Frankly, the subtraction and integer division is simpler.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yeah thats what i thought
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed