C - Remove result element

Asked

Viewed 62 times

2

How do I remove the 0.00000 printings in the response of that program after the functions idealBrutus and idealOlivia are called in the function main?

I realized that if, for example, I change line 59 from the original to:

printf ("%s", idealBrutus (imcBrutus,pBrutus, hBrutus, strBrutus));

I will receive a (null) after the function is idealBrutus be used.

#include <stdio.h>
#include <cmath>

float imc (float peso, float altura)
{
    return ( peso / pow(altura, 2));
}

float idealBrutus (float imc, float peso_inicial,float altura, char nome[100])
{
    float dif_para_o_peso_ideal;
    float peso_ideal;

    peso_ideal = 25 * pow(altura,2);

    dif_para_o_peso_ideal = abs(peso_ideal - peso_inicial);

    printf ("Para ficar com o Imc ideal %s ", nome);
    printf ("deve perder %f kg \n", dif_para_o_peso_ideal);

}

float idealOlivia  (float imc, float peso_inicial,float altura, char nome[100])
{
    float dif_para_o_peso_ideal;
    float peso_ideal;

    peso_ideal = 18.5 * pow(altura,2);

    dif_para_o_peso_ideal = abs(peso_ideal - peso_inicial);

    printf ("Para ficar com o Imc ideal %s ", nome);
    printf ("deve ganhar %f kg \n", dif_para_o_peso_ideal);

}

int main ()
{
    char strBrutus[100] = "Brutus";
    float hBrutus;
    float pBrutus;
    float imcBrutus;

    char strOlivia[100] = "Olivia";
    float hOlivia;
    float pOlivia;
    float imcOlivia;

    pBrutus = 122;
    hBrutus = 1.84;

    pOlivia = 45;
    hOlivia = 1.76;

    imcBrutus = imc (pBrutus, hBrutus);
    imcOlivia = imc (pOlivia, hOlivia);

    printf ("O imc de Brutus e : %f \n", imcBrutus);
    printf ("%f", idealBrutus (imcBrutus,pBrutus, hBrutus, strBrutus));
    printf ("O imc de Olivia e : %f \n", imcOlivia);
    printf ("%f", idealOlivia (imcOlivia,pOlivia, hOlivia, strOlivia));

}
  • Of all the variables you want to take out the houses after the , ?

  • When running the code I have the following result: Brutus' BMI and : 36.034969 To stay with the ideal Imc Brutus must lose 37.000000 kg 0.000000The Olivia’s BMI and : 14.527376 To stay with the ideal Imc Olivia must gain 12.000000 kg 0.000000

  • I wanted to get rid of these 0.000000 after the 37.000000 kg, so that the result is : 36.034969 To get the ideal Imc Brutus must lose 37.000000 kg Olivia’s BMI and : 14.527376 To get the ideal Imc Olivia must gain 12.000000 kg

  • I will elaborate the answer @Leandrosouza

2 answers

1


I was going through your code and I realized that you had a question list the houses after the , how to eliminate them.

So I tested this code and it worked:

#include <stdio.h>
#include <cmath>

float imc (float peso, float altura)
{
    return ( peso / pow(altura, 2));
}

float idealBrutus (float imc, float peso_inicial,float altura, char nome[100])
{
    float dif_para_o_peso_ideal;
    float peso_ideal;

    peso_ideal = 25 * pow(altura,2);

    dif_para_o_peso_ideal = abs(peso_ideal - peso_inicial);

    printf ("Para ficar com o Imc ideal %s ", nome);
    printf ("deve perder %.0f kg \n", dif_para_o_peso_ideal);

}

float idealOlivia  (float imc, float peso_inicial,float altura, char nome[100])
{
    float dif_para_o_peso_ideal;
    float peso_ideal;

    peso_ideal = 18.5 * pow(altura,2);

    dif_para_o_peso_ideal = abs(peso_ideal - peso_inicial);

    printf ("Para ficar com o Imc ideal %s ", nome);
    printf ("deve ganhar %.0f kg \n", dif_para_o_peso_ideal);

}

int main ()
{
    char strBrutus[100] = "Brutus";
    float hBrutus;
    float pBrutus;
    float imcBrutus;

    char strOlivia[100] = "Olivia";
    float hOlivia;
    float pOlivia;
    float imcOlivia;

    pBrutus = 122;
    hBrutus = 1.84;

    pOlivia = 45;
    hOlivia = 1.76;

    imcBrutus = imc (pBrutus, hBrutus);
    imcOlivia = imc (pOlivia, hOlivia);

    printf ("O imc de Brutus e : %.0f \n", imcBrutus);
    printf ("%.0f", idealBrutus (imcBrutus,pBrutus, hBrutus, strBrutus));
    printf ("O imc de Olivia e : %.0f \n", imcOlivia);
    printf ("%.0f", idealOlivia (imcOlivia,pOlivia, hOlivia, strOlivia));

}

What I did to work out the elimination of houses after the , ?

I added in printf("%. 0f");

Why use %.0f instead of just using %f?

When you put %. is limiting the houses after the .

  • 1

    Thank you very much for the answer, in fact the result was better without the decimals. Just an addendum, on lines 59 and 61 of your revised code, I put "%." instead of "%f" to prevent the result from printing 0.00000 after , for example, 37 kg, as suggested by the grandson’s comment.

  • Thanks for the comment. I’m glad I helped! I already edited the lines in the reply!

0

In the printf() just put the following formatting: "%.2f". The number indicates how many houses you want to show.

  • Thank you, switching to "%." instead of "%f" solved the problem.

Browser other questions tagged

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