计算新人口
Calculating a new population
在人口中,出生率是人口因出生而增加的百分比,死亡率是人口因死亡而减少的百分比。编写一个要求以下内容的程序:
- 种群的起始大小(最小 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
.
然后,从初始人口规模endPop
、deathRate
死亡率和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。
相关文章:
- 使用新行和不使用新行读取文件
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 遇到新行时,有没有办法停止istream_iterator
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 模板元编程:如何将参数包组合成新的参数包
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 如何使用CLion在Mac上创建一个新的.txt文件
- 错误-我无法在VS2019中打开新的Qt项目
- 有没有比在库中添加一个并非由所有派生类实现的新虚拟函数更好的设计实践
- 为什么新的随机库比std::rand()更好
- 在cygwin中测试新的boost安装时出现cpp错误
- C++:继续创建新的变量可以吗
- Qt:当QListView获得新条目时,如何更新QStringList
- 为C++03编译器编写部分unique_ptr,该编译器与较新的编译器在公共代码库上运行
- 被 c++ 中的瞬态人口程序难倒
- 我可以简化这个 std::list 的人口吗?
- 如何从我的结构中打印人口最多的状态
- 计算新人口