Search code examples
cwhile-loopcs50

The C program is not displaying any output but it take the input Question from CS50 Lab1


Say we have a population of n llamas. Each year, n / 3 new llamas are born, and n / 4 llamas pass away.

For example, if we were to start with n = 1200 llamas, then in the first year, 1200 / 3 = 400 new llamas would be born and 1200 / 4 = 300 llamas would pass away. At the end of that year, we would have 1200 + 400 - 300 = 1300 llamas.

#include <math.h>
#include <stdio.h>

int main(void)
{
    // TODO: Prompt for start size
    int a, b;
    do
    {
        printf("Start Size: ");
        scanf("%i", &a);
    }
    while (a < 9);
    // TODO: Prompt for end size
    do
    {
        printf("End Size: ");
        scanf("%i", &b);
    }
    while (b < a);
    // TODO: Calculate number of years until we reach threshold
    int y = 0;
    while (a < b)
    {
        a = round((float)a + (a / 4) - (a / 3));
        y++;
    }

    // TODO: Print number of years
    printf("Years: %i\n", y);
}

The Input I tried and output I expected:

  1. Start size: 1200
    End size: 1300
    Years: 1

  2. Start size: -5
    Start size: 3
    Start size: 9
    End size: 5
    End size: 18
    Years: 8

  3. Start size: 20
    End size: 1
    End size: 10
    End size: 100
    Years: 20

  4. Start size: 100
    End size: 1000000
    Years: 115


Solution

  • The formula is incorrect: you swapped the number of newly born llamas and the number of deceased ones. This formula converges to 0 and causes an infinite loop once a reaches a value between 0 and 2 and does not change anymore.

    Write this instead:

    a = round((float)a + (a / 3) - (a / 4));
    

    Note that you should use plain integer arithmetics as the number of new llamas and dead llamas are whole numbers. Since a in an int, so are a / 3 and a / 4, no need for a (float) cast or a round() function call:

    a = a + a / 3 - a / 4;
    

    or simpler:

    a += a / 3 - a / 4;  
    

    Note also that you should test the return value of scanf() to avoid undefined behavior in case of invalid or missing input.

    Here is a modified version:

    #include <stdio.h>
    
    int main(void)
    {
        // Prompt for start size
        int a, b;
    
        for (;;) {
            printf("Start Size: ");
            if (scanf("%i", &a) != 1) {
                int c;
                printf("invalid input\n");
                while ((c = getchar()) != EOF && c != '\n')
                    continue;
                if (c == EOF)
                    printf("unexpected end of file\n");
                    return 1;
                }
                continue;
            }
            if (a < 9) {
                printf("value must be at least 9\n");
                continue;
            }
            break;
        }
    
        // Prompt for end size
        for (;;) {
            printf("End Size: ");
            if (scanf("%i", &b) != 1) {
                int c;
                printf("invalid input\n");
                while ((c = getchar()) != EOF && c != '\n')
                    continue;
                if (c == EOF)
                    printf("unexpected end of file\n");
                    return 1;
                }
                continue;
            }
            if (b < a) {
                printf("value must be at least %d\n", a);
                continue;
            }
            break;
        }
    
        // Calculate number of years until we reach threshold
        int y = 0;
        while (a < b) {
            a += a / 3 - a / 4;
            y++;
        }
    
        // Print number of years
        printf("Years: %i\n", y);
        return 0;
    }
    

    As you are taking the CS50 course, you should use the function get_int() from <cs50.h> to simplify the error handling in a convenient manner:

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        // Prompt for start size
        int a, b;
    
        while ((a = get_int("Start Size: ")) < 9) {
            printf("value must be at least 9\n");
        }
    
        // Prompt for end size
        while ((b = get_int("End Size: ")) < a) {
            printf("value must be at least %d\n", a);
        }
    
        // Calculate number of years until we reach threshold
        int y = 0;
        while (a < b) {
            a += a / 3 - a / 4;
            y++;
        }
    
        // Print number of years
        printf("Years: %i\n", y);
        return 0;
    }