Cs50 Lab 1 population growth 2021: My solution and explanation

Stay Calm, In this post, I will explain how I was able to write and complete Cs50 Pset 1 lab solution in simple terms.

if you have any questions, use the comment box. I will get back to you asap. You can also check my problem set 1 solution here

in Lab 1, our task is to write a program that simulates population growth by determining how long a population reaches a particular size.

Step by Step Solution to Cs50 Lab 1 Population

Before we can start to implement the solution, make sure that you understand the problem, if you don’t understand, you can watch the walkthrough here.

Let’s start.

Prompting for Start population size

The first task here is to ask the user for the start population size and make sure the user does not input a value below 9, if at all the user did not cooperate then we should re-prompt the user until cooperate.

To get the user’s input, we will use the get_int(), a custom function that is defined in cs50.h, and then use the do-while loop to make sure the user cooperate and does not input a value below 9…..see the below code

    int start_size;
    do
    {
        start_size = get_int("Start Size: ");
    }
    while (start_size < 9);

Then we prompt the user for end population size

We will use the same get_int() function to prompt the user for ending population size and note: we are to make sure the user doesn’t input value below the start size. To do this we will use the do-while loop again….code below

int end_size;    //a variable for end_size
do
{
    end_size = get_int("End Size: ");
}
while (end_size < start_size);

Then Calculate the number of years until we reach the threshold (end population size)

A formula has been given us to use, the only thing needed here is for us to keep track of the years required, and to do this we will use the while loop to do the calculation and keep track of the population by incrementing a value that will be initially set to 0.

I Guess this pseudocode will explain better….see

  • declare and set a variable to 0 (so that we can keep track of the number of years as we go)
  • while the start size is less than the end size,
  • do the required calculation and increment the variable that was being set to 0.
    int years = 0;
    while (start_size < end_size)
    {
        start_size = start_size + (start_size / 3) -              (start_size / 4);
        years++;
    }

Lastly, printing the number of years

Just use the printf(); function to print the value (years) with the right placeholder.

printf("Years: %i\n", years);

That’s all…

Thanks for reading and I hope you are able to write and submit this Pset. Don’t forget to use style50 to check for the good design of your code.

In case you have any problems while implementing the solution, feel free to use the comment box below. You can also check my problem set 1 solution.

Have a nice day… I will appreciate it if you can add a comment below. Thanks

4 comments

  1. Hey why are you doing (start_size < 9); when the start siz should be greater or equal to 9.
    does < means greater than in coding?

    1. I’m using the “do while loop“, it literally means a case must be met before exiting. In this program, it means I must input a value that is greater than or equal to 9 else, the program will keep prompting me for a value.

      1. Hi! I’m new to cs50, my issue is that when I try to compile, I get an error saying “expression result unused” regarding the minus symbol between the two formulas. Is this a format issue on my part?

        1. I’m sorry, it seems I don’t get that. could you please explain more better.

Leave a Reply