How to use different heights for BMI in C?

Asked

Viewed 81 times

2

In my BMI calculation program (in C), you must type the mass first and then the height (of n people). I wonder why he is considering the masses correctly but is always using the first height to calculate.

#include<stdio.h>
#include<stdlib.h>

int main(void)

{

    float massa, altura, IMC;

    float *b=NULL;

    int counter = 2;

    int i, j;

    float somacounter;

    int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, counter5 = 0, counter6 = 0, counter7 = 0;

    b=(float*) malloc(2 * sizeof(float));

    scanf("%f",&massa);
    scanf("%f",&altura);

    b[0]=massa;
    b[1]=altura;

    int flag = 1;

    while(flag)
    {
        scanf("%f",&massa);
        scanf("%f",&altura);

        if(massa == 0.00 && altura == 0.00)
        {
            flag = 0;
            continue;
        }

         counter = counter + 2;
         b = (float*) realloc (b , (counter) * sizeof(float));

        b[counter - 2] = massa;
        b[counter - 1] = altura;

    }

   for(i = 0 ; i < counter - 1 ; i++)

   {

    for(j = 1 ; j < counter - 2; j = i + 1)

            {

                IMC = b[i]/(b[j]*b[j]);
                i++;
                j = i + 1;

                if(IMC<17)
                {
                    printf("\nMuito abaixo do peso");
                    counter1++;
                    break;
                }
                if(IMC>=17 && IMC<=18.49)
                {
                    printf("\nAbaixo do peso");
                    counter2++;
                    break;
                }
                if(IMC>=18.5 && IMC<=24.99)
                {
                    printf("\nPeso Normal");
                    counter3++;
                    break;
                }
                if(IMC>=25 && IMC<=29.99)
                {
                    printf("\nAcima do peso");
                    counter4++;
                    break;
                }
                if(IMC>=30 && IMC<=34.99)
                {
                    printf("\nObesidade I");
                    counter5++;
                    break;
                }
                if(IMC>=35 && IMC<=39.99)
                {
                    printf("\nObesidade II (severa)");
                    counter6++;
                    break;
                }
                if(IMC>=40)
                {
                    printf("\nObesidade III (Morbida)");
                    counter7++;
                    break;
                }

            }
   }

               somacounter = counter1+counter2+counter3+counter4+counter5+counter6+counter7;

               printf("\n\nMuito abaixo do peso: %d (%.2f )", counter1, counter1/somacounter * 100);
               printf("\nAbaixo do peso: %d (%.2f )", counter2, counter2/somacounter * 100);
               printf("\nPeso normal: %d (%.2f )", counter3, counter3/somacounter * 100);
               printf("\nAcima do peso: %d (%.2f )", counter4, counter4/somacounter * 100);
               printf("\nObesidade I: %d (%.2f )", counter5, counter5/somacounter * 100);
               printf("\nObesidade II (severa): %d (%.2f )", counter6, counter6/somacounter * 100);
               printf("\nObesidade III (morbida): %d (%.2f )", counter7, counter7/somacounter * 100);

return 0;

}
  • 2

    It’s a little hard to understand what your program is doing. The name of its variables is very confusing (counter7, "b", "flag"), has several redundant variables ("sum" and "height", "flag"+continue instead of break...), the masses and heights are all mixed in the same vector... Do you think you can produce a simpler version of your program that still plays your error? If you fool, during the simplification process you even find the mistake. It also costs nothing to put some printfs in the middle of the code to see what is happening :)

1 answer

3

I believe your mistake comes from break within the ifs, that breaks the for of the variable j (even so it is not recommended to use loops breaks).

Trade him for Else if in the next condition.

        if(IMC<17)
        {
            printf("\nMuito abaixo do peso");
            counter1++;
        }
        else if(IMC>=17 && IMC<=18.49)
        {
            printf("\nAbaixo do peso");
            counter2++;
        }
        else if(IMC>18.49 && IMC<=24.99)
        {
            printf("\nPeso Normal");
            counter3++;
        }

Another point, try naming variables with something you can understand without having to find their function in the code. For example, instead of counter1, counter2..., exchange them for counterNormal, counterNormal, counterObeso...

Browser other questions tagged

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