被 c++ 中的瞬态人口程序难倒

Stumped with a transient population program in c++

本文关键字:人口 程序 c++      更新时间:2023-10-16

这是我第一次在这里发帖。每当我遇到编程问题时,我通常都能找到足够的信息来解开我的困境。恐怕我遇到的问题,我找不到答案。这是我需要有人查看的东西,以告诉我我在代码中可能做错了什么。

我让程序成功运行,它确实有效。然而,问题是,与我的编程实验室的预期输出相比,我生成的输出相差了几个数字。我真的不确定该怎么做才能产生正确的输出。请允许我发布我的源代码和 MPL 结果屏幕的屏幕截图。

源代码:

#include <iostream>
using namespace std;
int populationCalculator(int, double, double, int, int);
int main()
{
int startingPopulation, newArrivals, peopleWhoLeft, years, 
newPopulation, finalPopulation;
double deathRate, birthRate;
cout << "This program calculates population change.n";
cout << "Enter the starting population size: ";
cin >> startingPopulation;
while (startingPopulation < 2)
{
cout << "nThe starting population may not be less than two. Please 
re - enter: ";
cin >> startingPopulation;
}
cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "nBirth rate percent cannot be negative. Please re - 
enter:";
cin >> birthRate;
}
birthRate = birthRate / 100;
cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "nDeath rate percent cannot be negative. Please  re - 
enter: ";
cin >> deathRate;
}
deathRate = deathRate / 100;
cout << "How many individuals move into the area each year? ";
cin >> newArrivals;
while (newArrivals < 0)
{
cout << "nArrivals cannot be negative. Please re - enter: ";
cin >> newArrivals;
}
cout << "How many individuals leave the area each year? ";
cin >> peopleWhoLeft;
while (peopleWhoLeft < 0)
{
cout << "nDepartures cannot be negative. Please re - enter: ";
cin >> peopleWhoLeft;
}
cout << "For how many years do you wish to view population changes? ";
cin >> years;
while (years < 1)
{
cout << "nYears must be one or more. Please re - enter: ";
cin >> years;
}
newPopulation = populationCalculator(startingPopulation, deathRate, 
birthRate, newArrivals, peopleWhoLeft);
cout << "nStarting population: " << startingPopulation << endl;
for (int loopCount = 1; loopCount <= years; loopCount++)
{
newPopulation = populationCalculator(newPopulation, deathRate, 
birthRate, newArrivals, peopleWhoLeft);
cout << "Population at the end of year " << loopCount << " is: " << 
newPopulation << endl;
}
system("pause");
return 0;
}
int populationCalculator(int Population, double deathRate, double birthRate, 
int newArrivals, int peopleWhoLeft)
{
int newPopulationCount;
newPopulationCount = Population + (Population * birthRate) - (Population 
* deathRate) + newArrivals - peopleWhoLeft;
return newPopulationCount;
}

MPL 结果:https://i.stack.imgur.com/lNbtl.jpg

如果有人能帮助我弄清楚为什么我生产的输出偏离了几个数字,我将不胜感激。

逐步完成您的代码。您返回的是一个具有双倍和整数乘法的 int。确保没有截断可能需要向上或向下舍入的值。

出生发生在死前还是死后?它应该分步发生,还是像你展示的那样一次发生?

尝试在涉及双精度和整数的操作中转换为双精度。例如

birthRate = (double)(birthRate / 100);
deathRate = (double)(deathRate / 100);

有时编程语言会将双精度转换为整数,这会导致您的数字关闭。

也许这个数字被四舍五入了? 我认为是这种情况,因为我注意到您在双精度中声明了一些变量,但最终结果是整数。尝试更改方法的数据类型populationCalculator()newPopulation,当然方法的返回值populationCalculator()整数双精度