Infinite Loop in Height Growth Analysis

0

Gustavo has 1.40 meters and grows G centimeters per year, while Juliano has 1.10 and grows by centimeters per year.

My program is giving an infinite loop when I execute.

#include<stdio.h>
int main()
{

float g,j,gus=1.40,jul=1.10;
int cont=0;

scanf("%lf %lf",&g, &j);

while(jul<=gus)
{
    jul=jul+j;
    printf("%lf\n",jul);

    gus=gus+g;
    printf("%lf\n",gus);

    cont++;

}
printf("%d",cont);


}
    
asked by anonymous 15.09.2018 / 01:10

1 answer

2

Declared as float use the formatting of Scanf() as %f .

You have to test to see if Gustavo grows smaller or equal to the Juliano, otherwise it will always be infinite, it is mathematical.

And you need to normalize. It's originally working with meters, and asks you to enter centimeters, so you need to divide by 100.

The most certain would be to do other validations, but for exercise this is good.

#include<stdio.h>

int main() {
    float g, j, gus = 1.40, jul = 1.10;
    int cont = 0;
    scanf("%f %f", &g, &j);
    if (g <= j) {
        printf("Nunca alcancará");
        return 0;
    }
    g /= 100;
    j /= 100;
    while (jul <= gus) {
        jul += j;
        printf("%lf\n", jul);
        gus += g;
        printf("%lf\n", gus);
        cont++;
    }
    printf("%d", cont);
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
15.09.2018 / 01:30