#include <iostream>
#include <cstdlib>
using namespace std;
// Function Prototype
void popCalculator(int &, int &, double &, double &);
void popFinder(int &, int &, int &, double &, double &);
int popArray[101];
int main()
{
int startingPop;
int population;
double birthRate;
double deathRate;
int numYears;
// These are the prompts for the user to enter the starting population,
// the annual birth rate, the annual death rate, and the number of years.
// Birth rate and death rate need to be entered as decimals.
cout << "This program calculates population change.\n";
// This is where you enter the starting population size and the annual birth
// rate. If the starting population is less than two, you have to enter
// a new starting population. Likewise, if the birth rate percent is
// less than 0, you have to enter a new one.
cout << "Enter the starting population size: Enter the annual birth rate" <<
"(as % of current population: ";
cin >> startingPop;
while (startingPop < 2)
{
cout << "Please re-enter: Starting population must be 2 or more.";
cin >> population;
}
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate percent cannot be negative.\n";
cout << "Please re-enter: ";
cin >> birthRate;
}
// This is where you enter the annual death rate population.
// If the death rate percent is less than 0, you have to enter a new one.
cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate percent cannot be negative.\n";
cout << "Please re-enter: ";
cin >> deathRate;
}
// This is where you enter the number of years to view the population
// changes over. The number can't be less than zero or greater than 100
// and you will be prompted to enter a new one if the number falls
// in either of those categories.
cout << "For how many years do you wish to view population changes? ";
cin >> numYears;
while (numYears < 0 || numYears > 100)
{
cout << "Years must be one or more, but less than or equal to 100.\n";
cout << "Please re-enter: ";
cin >> numYears;
}
// This prints out what the starting population is before the program
// prints out the rest of the population listings
cout << "Starting population: " << startingPop << endl << endl;
popFinder(startingPop, population, numYears, birthRate, deathRate);
}
void popCalculator(int &startingPop, int &population, double &birthRate, double &deathRate)
{
population = startingPop + (startingPop * birthRate) - (startingPop * deathRate);
population = population + (population * birthRate) - (population * deathRate);
}
void popFinder(int &startingPop, int &population, int &numYears, double &birthRate, double &deathRate)
{
int i;
for(i = 1; i <= numYears; i++)
{
popArray[] = population;
if (population < 0)
{
population = 0;
cout << "Population at the end of year " << i << " is " << static_cast<int>(population);
}
cout << "Population at the end of year " << i << " is " << static_cast<int>(population) << endl;
}
}
到目前为止我的代码,我几乎已经完成了。该计划背后的想法是获取起始人口,出生率,死亡率和年数的值,计算这些年份的人口,将这些值存储在一个数组中,然后打印出来。我现在的主要问题是,我似乎正在打印出我所假设的内存地址而不是值。我觉得我已经接近搞清楚了,但这部分让我感到非常难过,我不知道该怎么做。任何帮助将非常感谢!