0

I have given instructions to write some code and i have to follow the comments giving and i get stuck and some point so can anyone tell me what i did wrong

/*
* Returns the percent change from the original to the desired servings.
* The percent is expressed as a decimal. i.e.:
* 1 = 100%
* 0.5 = 50%
* 1.5 = 150%
* If either of the serving values is 0, a value of 1 serving is assumed.
*
* To calculate the number of servings, the following formula is used:
*
* Desired Servings =
* | Desired Servings / Original Servings | - 0.1 *
* (1 - | Original Servings / Desired Servings | ))
*
* where | x | is the absolute value of x
*
* original - number of servings the recipe was designed for
* desired - the number of servings wanted
*
* returns the % change that should apply to all of the ingredients.
*/
double changeServings(int original, int desired)
{
//stops any division by 0
if (original == 0)
{
original = 1;
}
if (desired == 0)
{
desired = 1;
}
//end check

// get absolute value for | Original Serving / Desired Servings |
double rate = original / (double)desired;
if (rate < 0)
rate = -rate;

//modify -1 below to be your new calculated value.
return (rate - 0.1 * (1 - rate));
}

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by ButterFly21
0

If | Original Servings / Desired Servings | = rate then
rate = 1 / | Desired Servings / Original Servings |, but you don't account for this in your code.

//modify -1 below to be your new calculated value.
return (rate - 0.1 * (1 - (1/rate)));

Edited by Intrade: Whoops, mixed up rate ;p

0

Sorry I forgot to change the line 7 from the bottom that starts off as // get absolute value for it should be desired servings/ original servings

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.