Im trying to output the population increase for a population. the problem is my for loop doesn't increment or add correctly. here is a sample output. i made it easy with no death rate and a population of 100 to easily calculate the increase in my head.

Starting Population: 100

Annual Birth Rate Percentage: 100
Birth as a percentage is: 1

Annual Death Rate Percentage: 0
death as a percentage is: 0

Number of years to calculate: 5
Year 1 population: 100
Year 2 population: 300
Year 3 population: 500
Year 4 population: 700
Year 5 population: 900

here is the for loop, where the problem is. the rest of the program is just asking for user input but i will include it if asked.

``````double population = startSize;
for (int i = 1; i <= years; i++)
{

cout << "Year " << i << "   population: " << population << endl;

population += (startSize * (1 + annualBirth) * (1 - annualDeath));

}``````
2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by m4ster_r0shi

I'm pretty sure the statement updating your population should be -> `population *= (1 + annualBirth - annualDeath);`

Edited by m4ster_r0shi: n/a

I'm pretty sure the statement updating your population should be -> `population *= (1 + annualBirth - annualDeath);`

Im working out of a book and doing the end of chapter exercises by myself and no solutions are provided but it gave the formula to use:

New population = Starting population * (1 + birth rate) * (1 - death rate)

i also changed the plus sign i had to the times sign and it keeps saying that

``error: expected initializer before '*=' token``

i think its because the compiler is wanting a pointer or something

I'm pretty sure the statement updating your population should be -> `population *= (1 + annualBirth - annualDeath);`

you're right i just changed it to population = population * ( 1 + annualBirth - annualDeath) and it works.

thanks.

although i am curious as to why it would give that pointer error when i tried *=

error: expected initializer before '*=' token

Hmmm... I don't know about that. This here works fine for me:

``````#include <iostream>

using namespace std;

int main()
{
double population = 100;

double annualBirth = 0.75;
double annualDeath = 0.25;

for (int i = 1; i <= 10; i++)
{
cout << "Year " << i << "   population: " << population << endl;

population *= (1 + annualBirth - annualDeath);
}

return 0;
}``````

New population = Starting population * (1 + birth rate) * (1 - death rate)

I see. Well, in C++ you could write this as -> `population *= (1 + annualBirth ) * (1 - annualDeath);` However, note that it's quite different than the one I suggested above.

Mine works like this:

``````temp1 = old_population * annualBirth;
temp2 = old_population * annualDeath;

new_population = old_population + temp1 - temp2;``````

``````temp1 = old_population * annualBirth;