I am stuck on a homework assignment where I must create a population growth calculator. The thing is that I have no idea how to make the calculation repeat so that it changes with each year.
#include <iostream>
usingnamespace std;
int newPopulation(double newPop, double startPop, double birthRate, double deathRate, double numYears)
{
birthRate = birthRate/100;
deathRate = deathRate/ 100;
newPop = startPop + (birthRate*startPop) - (deathRate*startPop);
startPop = newPop;
return startPop;
}
int main()
{
double startPop, birthRate, deathRate, numYears, newPop;
cout << "Welcome to the population calculator." << endl;
cout << "Please enter the starting size of the population." << endl;
cin >> startPop;
while(startPop < 2){
cout << "Error! Starting population must be 2 or more!"<< endl;
cout << "Please re-enter the starting population."<< endl;
cin >> startPop;
}
cout << "Please enter the annual birth rate."<< endl;
cin >> birthRate;
while (birthRate < 0)
{
cout << "Error! The birth rate cannot be negative!"<< endl;
cout << "Please re-enter the annual birth rate."<< endl;
cin >> birthRate;
}
cout << "Please enter the annual death rate."<< endl;
cin >> deathRate;
while (deathRate < 0)
{
cout << "Error! The death rate cannot be negative!"<< endl;
cout << "Please re-enter the annual death rate."<< endl;
cin >> deathRate;
}
cout << "Please enter the number of years to display." << endl;
cin >> numYears;
while (numYears < 1)
{
cout << "Error! The number of years must be one or more!"<< endl;
cout << "Please re-enter the number of years to display."<< endl;
cin >> numYears;
}
for(int i = 0; i < numYears; i++) {
cout << "The population for year " << i+1 << " will be: ";
}
return 0;
}
You already have a for loop at line 64 that will iterate through the specified number of years.
All you need to do is call your newPopulation() function.
1 2
// Add after line 65
startPop = newPopulation (startPop, startPop, birthRate, deathRate, numYears);
Line 5: newPop should be a local variable, not an argument.
Line 5: What's the point of passing numYears? Your function does not use it.
#include <iostream>
usingnamespace std;
int newPopulation(double startPop, double birthRate, double deathRate)
{
double newPop = 0;
birthRate = birthRate/100;
deathRate = deathRate/ 100;
newPop = startPop + (birthRate*startPop) - (deathRate*startPop);
startPop = newPop;
return newPop;
}
int main()
{
double startPop, birthRate, deathRate, numYears, newPop;
cout << "Welcome to the population calculator." << endl;
cout << "Please enter the starting size of the population." << endl;
cin >> startPop;
while(startPop < 2){
cout << "Error! Starting population must be 2 or more!"<< endl;
cout << "Please re-enter the starting population."<< endl;
cin >> startPop;
}
cout << "Please enter the annual birth rate."<< endl;
cin >> birthRate;
while (birthRate < 0)
{
cout << "Error! The birth rate cannot be negative!"<< endl;
cout << "Please re-enter the annual birth rate."<< endl;
cin >> birthRate;
}
cout << "Please enter the annual death rate."<< endl;
cin >> deathRate;
while (deathRate < 0)
{
cout << "Error! The death rate cannot be negative!"<< endl;
cout << "Please re-enter the annual death rate."<< endl;
cin >> deathRate;
}
cout << "Please enter the number of years to display." << endl;
cin >> numYears;
while (numYears < 1)
{
cout << "Error! The number of years must be one or more!"<< endl;
cout << "Please re-enter the number of years to display."<< endl;
cin >> numYears;
}
for(int i = 0; i < numYears; i++) {
cout << "The population for year " << i+1 << " will be: ";
cout << newPopulation(startPop, birthRate, deathRate) << endl;
}
return 0;
}
output:
Welcome to the population calculator.
Please enter the starting size of the population.
600
Please enter the annual birth rate.
3
Please enter the annual death rate.
4
Please enter the number of years to display.
5
The population for year 1 will be: 594
The population for year 2 will be: 594
The population for year 3 will be: 594
The population for year 4 will be: 594
The population for year 5 will be: 594