Population Program Problem

I'm new at C++ user-defined functions and my assignment is to figure out growth rate and estimated population. My only problem is in the estimated population which is to loop on how many years i've input (because that's what our professor said and not to multiply to the number of year but instead to loop and stack all the result) (example P = 100, B = 5, D = 2, N = 20 == the answer should be 168 but i'm getting 260 or 2060. Am I the one who is wrong or my professor? lol)

Here is the statement:
If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula:

P + B*P – D*P
----- ----- < division
100 100

The population growth rate is given by the formula:
B-D

Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must consist of the following functions:

a. growthRate: This function takes as its parameters the birth and death rates, and it returns the population growth rate.

b. estimated Population: This function takes as its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years.

Your program should not accept a negative birth rate, negative death rate, or a population less than 2.

This is my solution. Does it look like it is correct if not what am I missing? Thanks!

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>
#include <cstdlib>

using namespace std;

double growthRate (double B, double D);
int estimatedPopulation (int P, double B, double D, int N);

double total, overall = 0;

int main()
{
    int P, N;
    double B, D;

    cout << "Enter the starting population: ";
    cin >> P;
     while(P<2){
        system("cls");
        cout << "It should not be less than 2. \nEnter the starting population: ";
        cin >> P;
    }
    system("cls");
    cout << "Enter the starting population: " << P << endl;
    cout << "Enter birth rate: ";
    cin >> B;
    while(B<0){
        system("cls");
        cout << "It should not be a negative value. \nEnter birth rate: ";
        cin >> B;
    }
    system("cls");
    cout << "Enter the starting population: " << P << endl;
    cout << "Enter birth rate: " << B << endl;
    cout << "Enter death rate: ";
    cin >> D;
    while(D<0){
        system("cls");
        cout << "It should not be a negative value. \nEnter death rate: ";
        cin >> D;
    }
    system("cls");
    cout << "Enter the starting population: " << P << endl;
    cout << "Enter birth rate: " << B << endl;
    cout << "Enter death rate: " << D << endl;
    cout << "Enter number of years: ";
    cin >> N;
    growthRate(B, D);
    estimatedPopulation(P, B, D, N);
    cout << "\nEstimated population after " << N << " years: " << overall << endl;
    return 0;
}

double growthRate (double B, double D)
{
    total = B - D;
    return total;
}

int estimatedPopulation (int P, double B, double D, int N)
{
    int times = 0;
    double estPopulation = 0, neww = 0;

    while(times<=N){
        overall = overall + estPopulation;
        estPopulation = P + (total * P / 100); //Instead of Birth and Death, I just change it to growth rate. Is my formula correct?
        times++;
    }
    return overall;
}
Last edited on
*bump*
60
61
62
63
64
65
66
67
68
69
70
71
 int estimatedPopulation (int P, double B, double D, int N)
{
    int times = 0;
    double estPopulation = 0, neww = 0;

    while(times<=N){
        overall = overall + estPopulation;
        estPopulation = P + (total * P / 100); //Instead of Birth and Death, I just change it to growth rate. Is my formula correct?
        times++;
    }
    return overall;
}

Where did overall and total come from? Also, your logic is very off here. From line 66, assuming overall is supposed to store the overall population, then estPopulation must be the change in population for the current year. But from line 67, estPopulation is the total population after the first year (103).
Do not use global variables (variables defined outside functions). Get rid of line 9.
Replace line 48 and 49 with
1
2
double gr=growthRate(B, D);
int overall=estimatedPopulation(P, gr, N);

On line 56 define total as double.
estimated population should not depend on B or D, but on growth rate instead. The content should look like:
1
2
3
4
5
6
7
8
9
10
int estimatedPopulation (int P, double GR, int N)
{
   int pop=P;
   for(int i=1;i<=N;i++)
   {
      pop+=(int)(pop*GR);
      cout<<"Year "<<i<<" population "<<pop<<endl;
   }
   return pop;
}
Topic archived. No new replies allowed.