Population Growth C++ program

I got the hardest school assignment out of the whole class and we have started 2 weeks ago only. I need your guys help to make it out alive. I have some knowledge but not enough to make this program, every code I tried producing failed miserably.

Research the world number population as of now (7.7B with 1.2% growth every year) Write a program that will tell you the population growth in the next 75 years, take in mind that according to statistics it drops by 0.034% every year. Print the results in a table, where the first column says the numbers from 1-75 (2019-2094). Second column will show the population in each year, the third column will show the number of population growth in that year. (Optional : calculate after how many years will the population be doubled)

Here's what i tried doing without including my terrible code :
if Pv would be the population after "n" years, Pa the actual population, p the growth of population in %, and n the numbers of years, then i could build that program off of this formula : Pv = Pa (1+p)ⁿ. But I can't. Somebody help me !
Welcome to the forum.

You'll get help here, but you'll get much more if you observe a few rules:

1. be specific in what you're asking
2. try something before asking for help
3. do post coding attempts. Format the code using the code blocks (to be found under "Format:" to the right of where you enter your question). And DON'T go back and delete (or even change) the code in your post; save it for posterity.

Regarding your problem, I'd suggest you start by creating a few constants/variables:

- a variable for the current population
- a variable for the current growth rate
- a variable for the absolute growth for a given year
- a constant for the annual growth rate slowdown

These, a loop, and a print statement should be all you need for this exercise. I don't even see where C++ is necessary; standard C would suffice.

Good luck.
Please don't be shy to post what you've tried - that includes code. We don't want to help you do your homework. We want to help you learn. And that's more important than homework! ;)

Pv = Pa (1+p)ⁿ would have been appropriate if 'p', the rate, was constant. But that's not the case since you've been given "it drops by 0.034% every year".

There surely would be a mathematical formula for this calculation, (because I know there's a similar kinematic equation and that's all I could think of as regards to the math lol) but that's out of my scope.

But most probably and if you've been taught loops (probably have) then they want you to use a looping structure. That being said using a math formula if you know how to work it out is a great idea.

Anyways on with the problem now.

You've been given:
1)
Current Population: 7,7B
2) Growth Rate initially: 1.2%
3) Rate of decrease in Growth Rate: 0.034%

You need to:
1)
Find population after 75 years.
2) Print 75 rows of statistics in a tabular.
3) After how many years the popular would have doubled.

Analysis:
Finding the population after one year is fairly simple. You just need to add 1.2% of 7.7B to 7.7B
(i.e population = 7'000'000'000 + ( 7'000'000'000 * ( 12 / 100 ) ) )

Finding the population after two years is a bit different because the rate changed. You need to add 1.2% decreased by 0.034% (i.e 1.2% - 0.034 = 1.166) of 7.7B to the new population we calculated in the last paragraph.
(i.e population = old_population + ( old_population * ( 1.2 / 100 - 0.034 / 100 ) ) )

Observation:
It's observed that in the formula, there are two variables that keep changing.
1) Old population - We can plug this from previous calculation
2) Change in the rate (reduction) - We can calculate too by subtracting 0.034 from the previous rate.

Calculations: 1) For calculating the tabular column, we make use of the observations we have discussed.
2) For finding the population after 75 years we can use the result from tabular column
3) For calculating after how many years the population would have doubled we use the same formula.

Implementation:

This problem can be solved either recursively (don't worry if you haven't learn it yet) or iteratively (this is nothing but your do-while, while and for loops).

Let's analyze how we would do it iteratively:

We loop 75 times and we use the formula that we came up with and we maintain variables inheriting values from previous iterations of the loop.

PSEUDOCODE:

Declare variables: Current_population = 7'000'000'000, Current_rate = 1.2, Rate_change = -0.034 

Start loop for 75 times:

Current_population = Current_population + ( Current_population * ( Current_rate / 100 + Rate_change / 100 ) ) 

print: which year and Current_population, 

End of loop

Display: After 75 years, the population is : Current population
// This is because the loop ends after 75 years. Therefore the value of Current_population will be
holding the population after 75 years which is precisely what we need!


To calculate after how many years the population would double:

Declare: Years = 0, Initial_population = 7'000'000'000

Start loop:

Year = Year+1
Current_population = Current_population + ( Current_population * ( Current_rate / 100 + Rate_change / 100 ) ) 
// same formula as before

if Current_population >= 2*Initial_population
  BREAK OUT OF LOOP
Hi Grime -

My TA in CS3 (a million years ago) would have given me a B for that answer. In the interest of coding parsimony, I would recommend the elimination of any unnecessary math. A simple loop on the years, multiplying by the growth factor (which would be modified in the loop as well), with a test for the reaching of the 2X factor, seems sufficient.

I admit that this might be slightly more processor expensive, but these days, few people seem to car about that -- coding clarity is the #1 priority.

My TA in CS3 (a million years ago) would have given me a B for that answer. In the interest of coding parsimony, I would recommend the elimination of any unnecessary math. A simple loop on the years, multiplying by the growth factor (which would be modified in the loop as well), with a test for the reaching of the 2X factor, seems sufficient.

I didn't get what you said mzimmers. You said that you would recommend elimination of unnecessary math and in the next line you say using a simple loop on the years seems sufficient?

What I wrote is the general approach. The pseudocode. Of course you can merge two loops to make one but that's for OP to figure out.

I admit that this might be slightly more processor expensive, but these days, few people seem to car about that -- coding clarity is the #1 priority.

Here there are only two approaches. Either looping or using math.

Using a loop needs more than just variables, it needs programming logic. If OP were wanting to use math then he would have to figure it out on his own because all that requires is variables and plugging values, this is not a math forum it's a programming forum. Plus I suck at maths. 0_0

I didn't tell him to use my approach, it was just to show how it could be done with looping.

And by the way I don't think anybody would use looping structures over math for 'clarity' ;p!
The loop isn't really optional; the OP said he needed to generate an annual table. I can't think of a simpler way to do that than a loop. If all he needed to do were determine the 2X year, I'd agree.
I'm confused.. what was the unnecessary math you were talking about? .-. The suggestion that a loop could entirely be avoided with maths? Oh is that what you had meant in that other post? In that case I misunderstood.. but then again that's not unnecessary math but everybody has their opinions so I can't question you. *thumbs up*

If all he needed to do were determine the 2X year, I'd agree.

And what are you agreeing with?
Sorry; I can see I wasn't being clear. The gist of my point was that the OP's calculation, which involved exponentiation, was 1) unnecessary, and 2) more complicated than the problem called for (IMO).

Regarding the use of a loop: the problem could technically be solved without one, but the code wouldn't be nearly as clean.

pseudo-code
1
2
3
4
5
6
7
8
9
10
for (int year = 0 to 75)
{
    current population is last year's * growth factor
    growth factor is reduced by declining factor
    if (current year is 2X original year)
    {
        this is the year to remember.
    }
    print year, population, etc.
} 


Solves the problem with arithmetic, a loop and a test. The only flaw I can see to this is the test gets run every iteration, but this is minor, and probably beyond the scope of the OP's current instruction.

All just IMO of course.
Yes exponentiation is both unnecessary and wrong. OP's calculation was wrong like I had said, because it hadn't considered change in Growth Rate. OP used formula for compound interest, which would have been correct if Growth Rate didn't change.

The math formula I was talking about was this:

Consider the Kinematic equation:
Displacement = Initial velocity x time + 1/2 acceleration x time x time

In our case,
Displacement = Change in Population
Initial velocity = Growth Rate
Acceleration = Rate of Change of Growth Rate

Seems legit.. I dunno.

So to calculate population after two years,
Population = 7'000'000'000 + ( (1.2/100*2) + ( 1 / 2 * (-0.034/100) * t * t )

I feel like I've gone wrong somewhere.

edit: Yea I just realized that the formula isn't right because Growth Rate should be an absolute value not a relative one. And in our case it's a relative. I had a bad feeling because if this were a correct formula then we wouldn't have to exponent for compound interest at all.

Anyways in like two weeks I'll be very very free. I'm determined to find a formula for this even if it has to be an estimation. My maths sucks for now though. :'(
Last edited on
Initialise:
   population:   P = 7.7e9
   growth rate:  r = 0.012                    (as decimal fraction of population)
   annual fall in growth rate:  f = 0.00034   (as decimal fraction of ?)

LOOP on year:
   dp <--  P * r
   P  <--  P + dP
   r  <--  r - f    (or possibly r * (1-f)  depending on how you read it)
   Write out year, P and dP in three columns



"% fall in growth rate" is ambiguous.
Last edited on
lastchance: exactly. Throw in a test for reaching 2x the original population, and print that line in bold or something, and you have a complete solution.
mzimmers wrote:
lastchance: exactly. Throw in a test for reaching 2x the original population, and print that line in bold or something, and you have a complete solution.


Try it, though - the ambiguous wording in "percentage fall in growth rate" makes quite a lot of difference ... is it:
0.034% of the population; i.e. r <-- r - f (for which population never doubles)
or
0.034% of the growth rate; i.e. r <-- r ( 1 - f ) (for which r barely changes)
Last edited on
Agreed. This problem description highlights another good software engineering principle: all the clever programming in the world can be undone by a vague, ambiguous problem specification. Either way, though, the solution logic is valid.
Then again how else would you have phrased it?

If it were the case that the Growth Rate was decreasing by 0.034% of its current value then they would have phrased it like "according to statistics the Growth Rate drops by 0.034% of itself every year".

How do you think they should've phrased it otherwise?
Anyways there seems to be more people talking about OP's question than OP himself!
Try it both ways, @Grime, and you will see the issue.
Try what?
Topic archived. No new replies allowed.