Inequality in C language

Asked

Viewed 266 times

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 how much MINIMAL of pounds that Brutus and Olivia must lose/gain to achieve a healthy weight according to BMI rating.

I thought to use an inequality, as in the example below for Brutus:

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

I did a quick search and I couldn’t find an inequality function in the library math.h. 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);

OBS: I must create the algorithm using very basic knowledge of C (do not include conditional commands, repeat commands, array, array, etc...)

OBS2: Exits:

  • Brutus needs to lose at least 98 pounds.

  • Olivia needs to gain at least 12.31 kilos.

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

  • 1

    Time normally will not be so absurdly distinct. Mathematical calculations of primitive types are performed in constant time. When trying to do something more efficient, one usually tries to decrease the execution complexity of the algorithm; for example, to sort o(n log n) is much more efficient than o(n^2), so it doesn’t make much sense to question how to brush bits to make quadratic ordering more efficient

  • 1

    They recently asked a question about performance between an algorithm o(n^2) and another o(n log n). I shared my results and the methodologies used. Look at the difference between the performance of selection sort to the merge (3). This is the power to decrease the complexity of the algorithm that solves the problem

1 answer

6


For definition of BMI, healthy would be 18.5 to 25.

First, let’s create a function to calculate the BMI:

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 won’t even need it here):

int imc_saudavel(float imc) {
    return imc >= 18.5 && imc <= 25;
}

If so-and-so has a low BMI, how much weight he needs to gain to get at 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 above 25, how much he has 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

whereas 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 gain 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 ifs and the use of ternary operator. The rest is based on equations.

I can eliminate that if?

If the person already has an ideal weight, we could eliminate the if by using as fator, own valorImc. Since in that case:

valorImc = peso / (altura * altura)

Then the return would produce this:

(peso / (altura * altura)) * altura * altura - peso

Note that peso is divided by altura * altura and then multiplied by that same term. These two operations cancel out, so that this results in:

peso - peso

Which is obviously always zero. So, if the person already has the ideal weight, we can use the valorImc in place of fator. This makes the code a little 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;
}

I can eliminate these ternary operators?

The function of these ternary operators is to ensure that the factor is within the healthy range of 18.5 through 25. Therefore, you can replace them by using the fminf and the fmaxf (don’t 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 don’t want to use functions that are not already ready in the standard library as well:

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 have as a result a code that uses only very basic knowledge in C.

  • 2

    I really liked this explanation +1

  • Taking the case of Brutus as an example, I’ll stick to solving the msm equation, without even using the library math.h. I mean, I’ll use the - peso_a_perder = 25 * altura * altura - peso_atual

  • The explanation was very good!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.