By the definition of BMI , healthy would be 18.5 to 25.
First, let's create a function to calculate the IMC:
float imc(float peso, float altura) {
return peso / (altura * altura);
}
We could also create a function that says whether the BMI is healthy or not (but we will not even need it here):
int imc_saudavel(float imc) {
return imc >= 18.5 && imc <= 25;
}
If so has a low BMI, how much weight does he need to gain to reach 18.5?
Let's solve the equation:
imc(peso_atual + peso_a_ganhar, altura) = 18,5
(peso_atual + peso_a_ganhar) / (altura * altura) = 18,5
peso_atual + peso_a_ganhar = 18,5 * altura * altura
peso_a_ganhar = 18,5 * altura * altura - peso_atual
And if it's over 25, how much does it have to lose?
imc(peso_atual - peso_a_perder, altura) = 25
(peso_atual - peso_a_perder) / (altura * altura) = 25
peso_atual - peso_a_perder = 25 * altura * altura
-peso_a_perder = 25 * altura * altura - peso_atual
Considering that peso_a_perder = -peso_a_ganhar
, then:
peso_a_ganhar = 25 * altura * altura - peso_atual
In both cases, we have this:
peso_a_ganhar = imc_ideal * altura * altura - peso_atual
So we can do a function that says how much weight a person should win or lose:
float peso_a_variar(float peso, float altura) {
float valorImc = imc(peso, altura);
if (valorImc >= 18.5 && valorImc <= 25) return 0; // A pessoa já tem o IMC saudável.
float fator = valorImc < 18.5 ? 18.5 : 25;
return fator * altura * altura - peso;
}
Note the inequalities in these if
s and the use of the ternary operator. The rest is based on equations.
Can I delete this if
?
If the person already has an ideal weight, we could eliminate if
by using as fator
, valorImc
itself. Since in this case:
valorImc = peso / (altura * altura)
Then return
would produce this:
(peso / (altura * altura)) * altura * altura - peso
Note that peso
is divided by altura * altura
and then multiplied by the same term. These two operations are aborted, so this results in:
peso - peso
Which of course is always zero. So if the person already has ideal weight, we can use valorImc
instead of fator
. This leaves the code a bit simpler:
float peso_a_variar(float peso, float altura) {
float valorImc = imc(peso, altura);
float fator = valorImc < 18.5 ? 18.5 : valorImc > 25 ? 25 : valorImc;
return fator * altura * altura - peso;
}
Can I delete these ternary operators?
The function of these ternary operators is to ensure that the factor is within the healthy range of 18.5 to 25. Therefore, you can override them by using fminf
and fmaxf
(do not forget the #include <math.h>
):
float peso_a_variar(float peso, float altura) {
float valorImc = imc(peso, altura);
float fator = fmaxf(18.5, fminf(valorImc, 25.0));
return fator * altura * altura - peso;
}
If you do not want to use functions that are not already ready in the default library either:
float peso_fulano = ...;
float altura_fulano = ...;
float imc_fulano = peso_fulano / (altura_fulano * altura_fulano);
float fator_fulano = fmaxf(18.5, fminf(imc_fulano, 25.0));
float peso_a_ganhar_fulano = fator_fulano * altura_fulano * altura_fulano - peso_fulano;
And you get a code that uses just basic C knowledge.