0
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int start, end, years = 0;
    // Prompt for start size
    do
    {
        start = get_int("Start size: ");
    }
    while (start < 9);
    // Prompt for end size
    do
    {
        end = get_int("End size: ");
    }
    while (end < start);
    //Calculating the number of years
    while (start < end)
    {
        start += (start / 2);
        years++;
    }
    printf("Years: %i\n", years);
}

output:

$ ./population
Start size: 1200
End size: 1300
Years: 1
$ ./population
Start size: -5
Start size: 3
Start size: 9
End size: 5
End size: 18
Years: 8
$ ./population
Start size: 20
End size: 1
End size: 10
End size: 100
Years: 20
$ ./population
Start size: 100
End size: 1000000
Years: 115
4
  • This is called a naked code drop - posting code with no explanation of any problem or issue and is against forum rules. Please edit the question, explaining exactly what you are asking, what the problem may be, and include discussion of what you think is wrong and what you have done to try and resolve it.
    – Cliff B
    Commented Jan 18, 2023 at 0:08
  • I'm sorry this is mt first time using stack exchange
    – shane
    Commented Jan 18, 2023 at 5:51
  • OK, no problem. Everyone has to learn how sometime. So, what's your question?
    – Cliff B
    Commented Jan 18, 2023 at 7:04
  • thank you for your help, in the code above it does not return the answers when put these into terminal theses is the behaver i need to make my code do $ ./population Start size: 1200 End size: 1300 Years: 1 $ ./population Start size: -5 Start size: 3 Start size: 9 End size: 5 End size: 18 Years: 8 $ ./population Start size: 20 End size: 1 End size: 10 End size: 100 Years: 20 $ ./population Start size: 100 End size: 1000000 Years: 115
    – shane
    Commented Jan 18, 2023 at 8:22

1 Answer 1

0

So, the code is actually running, but not producing the correct answers, correct?

Look at the code for the while loop that is supposed to calculate the number of years required:

while (start < end)
{
    start += (start / 2);
    years++;
}

It says that the population increased by 1/2 each year. Where did you get this from?

The program spec says that the population increases by 1/3 each year from births and decreases by 1/4 from deaths. That's not a net change of 1/2. Simply put, the formula that calculates annual increase is wrong. Also, don't try to simplify the formula to simply add a certain amount. It will introduce rounding/truncation errors.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • thank you so much for your help, i fully understand the problem and bt cant figure out correct way to implement this match, thanks again for you help
    – shane
    Commented Jan 19, 2023 at 11:34
  • it's simple. The spec says "Each year, n / 3 new llamas are born, and n / 4 llamas pass away." So, start += (start /3) - (start/ 4);
    – Cliff B
    Commented Jan 19, 2023 at 12:23
  • Thanks again for your help.
    – shane
    Commented Jan 23, 2023 at 12:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .