Untitled

 avatar
user_2604787
plain_text
2 years ago
1.1 kB
2
Indexable
#include <cs50.h>
#include <stdio.h>

int get_start(void);
int get_end(void);
int calculate_years(int s, int e);

int main(void)
{
    // TODO: Prompt for start size
    int s = get_start();
    // TODO: Prompt for end size
    int e = get_end();
    // TODO: Calculate number of years until we reach threshold
    int years = calculate_years(s, e);
    // TODO: Print number of years
    printf("Years: %i\n", years);
}

int get_start(void)
{

    // Get the start size
    int s;
    do
    {
        s = get_int("Start Population: ");
    }
    while (s < 9);
    return s;
}

int get_end(void)
{

    // Get the end size
    int e;
    int s = get_start();

    do
    {
        e = get_int("End Population: ");
    }
    while (e < s); // error: (e < s) returns two start population prompts
    return e;
}

int calculate_years(int s, int e)
{
     // Calculate the number of years
    int years = 0;
    int n = s;

    while (n < e)
    {
        n = n + (n / 3) - (n / 4); years++;
    }

    return years;
}

// n = n + (n/3) - (n/4);
// printf("Years: %i\n", n);
Editor is loading...