Inequality in C language

2

Data: Brutus is 1.84m and weighs 122kg and Olivia is 1.76m and weighs 45kg.

IMC = peso/(altura*altura);

IMC: 18,5 a 25 -----> Saudável

I want to create an algorithm that prints the MINIMUM amount of pounds that Brutus and Olivia must lose / gain to reach a healthy weight according to the BMI classification.

I thought of using an inequality, as in the example below for Brutus:

peso_a_perder = peso - x / (altura * altura) <= 25;

I did a quick search and did not find an inequality function in the math.h library. Below is part of the code.

float imc_Brutus, imc_Olivia, peso_Brutus = 122, altura_Brutus = 1.84, peso_Olivia = 45, altura_Olivia = 1.76;
/*
O calculo do IMC dos dois esta aqui (omisso)
*/

float x, peso_a_perder_Brutus, peso_a_perder_Olivia;
peso_a_perder_Brutus = peso_Brutus - x / (altura_Brutus * altura_Brutus) <= 25;
printf("Brutus deve perder %0.2f kg para atingir um peso saudavel\n", peso_a_perder_Brutus);
printf("Olivia deve ganhar %0.2f kg para atingir um peso saudavel\n", peso_a_perder_Olivia);

I need to create the algorithm using very basic knowledge of C (not include conditional commands, repetition commands, vector, array, etc ...)

OBS2: Outputs:

  
  • Brutus needs to lose at least 37.36 pounds.

  •   
  • Olivia needs to earn at least 12.31 pounds.

  •   

Extra question: What would be the most efficient method to solve this calculation? Inequality itself, or something else ?

    
asked by anonymous 24.08.2017 / 17:55

1 answer

6

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.

    
24.08.2017 / 18:55