# Thread: can i do this some other way?

1. ## 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;
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;

}```

2. Perhaps you could write it this way:
Code:
```//function to compute jacket size
double jacketsize(double height, double weight, int age)
{
double jacket = (height * weight) / 288.0;
if (age >= 40)
jacket += static_cast<double>((age - 30) / 10) * adjust;
return jacket;
}```

3. 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);`

4. swoopy: what happens to your algorithm is age = 29? count will turn negative and the adjustments will be incorrect.

5. 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. Ancient Dragon and Bench, good point.

7. You might want to make the adjust variable a static constant
Code:
`static const double adjust = 0.125;`

8. 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 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.

9. 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.

10. yeah thats what i thought