计算新人口

Calculating a new population

本文关键字:人口 新人口 计算      更新时间:2023-10-16

在人口中,出生率是人口因出生而增加的百分比,死亡率是人口因死亡而减少的百分比。编写一个要求以下内容的程序:

  • 种群的起始大小(最小 2((提示输入起始大小:(
  • 年出生率
  • (提示输入年出生率:(
  • 年死亡率
  • (提示输入年死亡率:(
  • 要显示的年数
  • (最少 1((提示输入要显示的年数:(

然后,程序应显示每年年底的起始人口和预计人口。它应该使用一个函数来计算并返回一年后预计的新人口规模。公式为

N = P(1 + B)(1 - D)

其中N是新的人口规模,P是以前的人口规模,B是出生率,D是死亡率。年出生率和死亡率是每 1000 人一年中的典型出生和死亡人数,以小数表示。

因此,例如,如果给定人口中每 1000 人中通常有大约 32 名新生儿和 26 人死亡,则出生率为 0.032,死亡率为 0.026。

这是我的代码;我无法弄清楚如何进行计算。

#include "stdafx.h"  // Defines IDE required "pre-compiled" definition files
#include <iomanip> 
#include <iostream>
using namespace std
int main ()
{
    double startPop, // To hold the starting population. 
    float   birthRate, // To hold the birth rate. 
        float deathRate; // To hold the death rate. 
    int numYears; // To hold the number of years to track population changes. 
                  // Input and validate starting population 
    cout << "This program calculates population change.n";
    cout << "Enter the starting population size: ";
    cin >> startPop;
    while (startPop < 2.0)
    {
        cout << "Starting population must be 2 or more. Please re-enter: ";
        cin >> startPop;
    }
    // Input and validate annual birth and death rates 
    cout << "Enter the annual birth rate (as % of current population): ";
    cin >> birthRate;
    while (birthRate < 0)
    {
        cout << "Birth rate percent cannot be negative. Please re-enter: ";
        cin >> birthRate;
    }
    birthRate = birthRate / 100; // Convert from % to decimal. 
    cout << "Enter the annual death rate (as % of current population): ";
    cin >> deathRate;
    while (deathRate < 0)
    {
        cout << "Death rate percent cannot be negative. Please re-enter: ";
        cin >> deathRate;
    }
    deathRate = deathRate / 100; // Convert from % to decimal. 
    cout << "For how many years do you wish to view population changes? ";
    cin >> numYears;
    while (numYears < 1)
    {
        cout << "Years must be one or more. Please re-enter: ";
        cin >> numYears;
        population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
        cout << population << endl;
        populationStartingSize = population;
    }
    printPopulations(startPop, birthRate, deathRate, numYears);
    return 0;
} // End of main function

您可以递归或使用简单的for循环执行此操作。

例如,假设 numYears = 10,您可能希望循环 10 次。

for循环之前创建一个临时变量,并为其分配startPop的值,例如 endPop .

然后,从初始人口规模endPopdeathRate死亡率和birthRate出生率开始,计算一年后的人口规模。

在第一个循环中计算一年后的人口后,使用新值更新endPop

随后,在第二个循环中,您再次使用endPop作为新的起始种群大小,并且循环重复,直到for循环结束,即 10 年过去时。

在使用变量之前,您没有在上面的代码片段中声明变量population

实现:

while (numYears < 1)
{
    cout << "Years must be one or more. Please re-enter: ";
    cin >> numYears;   
}
double population = populationStartingSize;
for ( int i = 0; i < numYears; i++) {
    population = projectedNewSize(population, annualBirthRate, annualDeathRate);
    cout << "After " << i+1 << "years: " << population << endl;
    }
}

请注意,如果您的数字太小或太大,则有可能溢出和不足。

实现:

double projectedNewSize(double populationStartingSize, float annualBirthRate, float annualDeathRate) {
    return populationStartingSize * (1 + annualBirthRate) * (1 - annualDeathRate);
}

对于numYears的读取,您可以考虑使用do-while循环,:p。