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

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.