Population Growth Function

I am stuck on a homework assignment where I must create a population growth calculator. The thing is that I have no idea how to make the calculation repeat so that it changes with each year.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;


int newPopulation(double newPop, double startPop, double birthRate, double deathRate, double numYears)
{
  
  birthRate = birthRate/100;
  deathRate = deathRate/ 100;
  
  newPop = startPop + (birthRate*startPop) - (deathRate*startPop);
  startPop = newPop;
  
  return startPop;
}


int main()
{

   double startPop, birthRate, deathRate, numYears, newPop;

   cout << "Welcome to the population calculator." << endl;

   cout << "Please enter the starting size of the population." << endl;
   cin >> startPop;

   while(startPop < 2){
   cout << "Error! Starting population must be 2 or more!"<< endl;
   cout << "Please re-enter the starting population."<< endl;
   cin >> startPop;
   }

   cout << "Please enter the annual birth rate."<< endl;
   cin >> birthRate;

   while (birthRate < 0)
   {
   cout << "Error! The birth rate cannot be negative!"<< endl;
   cout << "Please re-enter the annual birth rate."<< endl;
   cin >> birthRate;
   }
	
   cout << "Please enter the annual death rate."<< endl;
   cin >> deathRate;

   while (deathRate < 0)
   {
   cout << "Error! The death rate cannot be negative!"<< endl;
   cout << "Please re-enter the annual death rate."<< endl;
   cin >> deathRate;
   }
			
   cout << "Please enter the number of years to display." << endl;
   cin >> numYears;

   while (numYears < 1)
   {
   cout << "Error! The number of years must be one or more!"<< endl;
   cout << "Please re-enter the number of years to display."<< endl;
   cin >> numYears;
   }

   for(int i = 0; i < numYears; i++) {
   cout << "The population for year " << i+1 << " will be: ";
   }

  return 0;

}
You already have a for loop at line 64 that will iterate through the specified number of years.
All you need to do is call your newPopulation() function.

1
2
//  Add after line 65
  startPop = newPopulation (startPop, startPop, birthRate, deathRate, numYears);


Line 5: newPop should be a local variable, not an argument.

Line 5: What's the point of passing numYears? Your function does not use it.

updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#include <iostream>
using namespace std;


int newPopulation(double startPop, double birthRate, double deathRate)
{
        double newPop = 0;
  
	birthRate = birthRate/100;
	deathRate = deathRate/ 100;
  	
	newPop = startPop + (birthRate*startPop) - (deathRate*startPop);
	startPop = newPop;
  
	return newPop;
}


int main()
{

	double startPop, birthRate, deathRate, numYears, newPop;

	cout << "Welcome to the population calculator." << endl;

	cout << "Please enter the starting size of the population." << endl;
	cin >> startPop;

	while(startPop < 2){
        cout << "Error! Starting population must be 2 or more!"<< endl;
        cout << "Please re-enter the starting population."<< endl;
        cin >> startPop;
	}

	cout << "Please enter the annual birth rate."<< endl;
  	cin >> birthRate;

   	 while (birthRate < 0)
    	{
        cout << "Error! The birth rate cannot be negative!"<< endl;
        cout << "Please re-enter the annual birth rate."<< endl;
        cin >> birthRate;
    	}
	
	cout << "Please enter the annual death rate."<< endl;
    	cin >> deathRate;

   	 while (deathRate < 0)
    	{
        cout << "Error! The death rate cannot be negative!"<< endl;
        cout << "Please re-enter the annual death rate."<< endl;
        cin >> deathRate;
    	}
			
	cout << "Please enter the number of years to display." << endl;
	cin >> numYears;

	while (numYears < 1)
    	{
        cout << "Error! The number of years must be one or more!"<< endl;
        cout << "Please re-enter the number of years to display."<< endl;
        cin >> numYears;
    	}


	for(int i = 0; i < numYears; i++) {
    	cout << "The population for year " << i+1 << " will be: ";
      cout << newPopulation(startPop, birthRate, deathRate) << endl;
    	}

    return 0;

}


output:


Welcome to the population calculator.
Please enter the starting size of the population.
 600
Please enter the annual birth rate.
 3
Please enter the annual death rate.
 4
Please enter the number of years to display.
 5
The population for year 1 will be: 594 
The population for year 2 will be: 594
The population for year 3 will be: 594
The population for year 4 will be: 594
The population for year 5 will be: 594



How do I get the population to change each year?
Last edited on
67
68
69
70
71
  for(int i = 0; i < numYears; i++) 
  {  cout << "The population for year " << i+1 << " will be: ";
      startPop = newPopulation(startPop, birthRate, deathRate);  // Update startPop
      cout << startPop << endl;
   }


Note that startPop is a double in main, but newPopulation() returns an int.
That's fine, because you probably don't want a fractional population.

Topic archived. No new replies allowed.