BRIAN YU: Let's take a look at how you might have solved the population growth
problem.
Recall that in this problem, your task was
to write a program in C that was able to figure out how many years it would take
to go from some starting population size to some ending population size,
according to some mathematical formula.
So inside of population dot C, you'll see
that the first thing we wanted to do was prompt the user
for a starting population size.
To do so, we could use the get int function from the CS50 Library,
saying get int Start size.
Let's get the starting size for the population,
storing that value inside of Start.
But recall that we couldn't just accept any integer as the starting size.
We definitely didn't want to accept 0 or a negative number.
But we also didn't want to accept numbers that were less than 9.
We needed the starting population to be at least 9
to make sure we could start to actually grow this population.
So in order to continually re-prompt the user for input
until they provide us with a valid input,
we here used a do-while loop, where we're
going to repeatedly prompt the user for a start size
as long as Start is less than 9.
If Start is less than 9, that means the input is not valid,
which means we need to prompt the user again.
And so this loop will continue running until we ultimately
get a value for Start that we consider to be valid.
After we've prompted for the starting population size,
we next need to prompt for the ending population size.
And here, too, there's very similar logic.
We want to use get int to prompt the user for an integer,
but we also want to make sure that integer is valid.
In particular, the ending population size
can't be less than the starting population size.
If the end size is less than the start size, that's not a valid input.
And so we need to re-prompt the user.
So here too, we can use a do-while loop.
We start by declaring an integer called End.
And then we're repeatedly going to prompt the user for input.
End equals get int End size.
And if end is less than start, that means the end value provided
by the user is not a valid integer, which
means we need to re-prompt the user for a new value again and again,
until they provide us with a valid end value.
Once we have a valid start and end value,
we now need to figure out how many years it
would take to get from the starting population size
to the end population size.
In doing so, we're going to want to keep track of how many years have passed.
And any time we want to keep track of information inside of our computer
program, it's often going to be helpful to have a variable to do just that.
So here we define an integer called years and set it equal to 0 at first,
because, initially, no years have passed so far.
But we're going to be updating that variable in due time.
Now we need to keep updating the population size
until we get to the end population size.
And any time we're doing something again and again, it's
often going to be helpful to put that code inside of a loop.
So "while start is less than end" is going
to repeatedly run some code, as long as the starting population hasn't yet
reached the end population.
Once the start population does reach the end population,
then this condition will be false and will exit the loop.
What does the loop do?
Well, here, we have a line of code that updates the population,
updates the value of start, based on the mathematical formula
for how many new members of the population we're gaining
and how many members of the population we're losing.
Recall that per the formula, every year, we gain start divided by 3 llamas.
We started with start llamas.
And we lose start divided by 4 llamas.
So by having start plus start over 3 minus start over 4,
we're able to calculate the updated value for the population size.
Recall, too, that division of integers in C
will automatically truncate for us anything
that shows up after the decimal.
So if we end up with a number like 2.8, for example, we'd cut off the 0.8.
And we'd just be left with the number 2, which is the behavior that we
expect for the program.
The other thing this loop needs to do, though,
in addition to updating the population size,
is to update how many years have gone by.
We've updated the population once, which means one additional year has passed.
And so I've included years plus plus here to mean one additional year
has passed.
Let's increase the value of the variable years by 1.
After this loop is over, we will have reached the end population size.
And the variable years will now store the number of years
that it took to get us from that starting size to the end size.
So ultimately, we can print using print f, Years colon, and then percent i
to stand in for some integer here printing out the value of years.
All in all, once we compile and run that program,
we can provide as input some start and end population size,
and our program will tell us how many years it took to get from the start
to the end size in population.
My name is Brian.
And this was population growth.