How to use different heights for IMC in C?

2

In my BMI calculation program (in C), you must enter the mass first and then the height (of n people). I would like to know 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;

}
    
asked by anonymous 26.04.2015 / 16:54

1 answer

3

I believe your error comes from break within the ifs , which breaks the for variable j ( so even the use of loop breaks is not recommended.)

Change it to 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 that you can understand without having to find their function in the code. For example, instead of counter1, counter2 ..., change them by counterAbout, counterNormal, counterObeso ...

    
26.04.2015 / 23:00