This is my program, ////////////////////Start of Program to monitor Population//
ID: 3760280 • Letter: T
Question
This is my program,
////////////////////Start of Program to monitor Population////////////////////
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//declare function prototype
void getInput(int &, double &, double &, double &); // get starting population, birth rate, death rate, and time in years data function
void endPopulationFunction(int &, double &, double &);
double calculatePop(int, double, double);//calculate the population
double population(double);
double yearsToDouble(double); //+ve growth rate
double yearsToZero(int , double);//-ve growth rate
double sPopulation(int);
void outputTable(int, double, double, double, int); // calculate and loop for the projected population data and prints to screen function
// start main function
int main()
{
//Keep asking user for input until Y
char ans;
do
{
//declare and initalize variables
int sPopulation=0;
double birthRate=0;
double deathRate=0;
double population=0;
double time = 0;
double growthRate=0;
int numRaw = 0;
//display title and purpose
cout << " ";
cout << " Welcome to the Population Monitor Program! ";
cout << " Projects population based on starting population, birth rate, ";
cout << " death rate, and time in years. Displays a table to show growth ";
cout << " or decline of the population. ";
// call functions
getInput(sPopulation, birthRate, deathRate, time);
endPopulationFunction(sPopulation, birthRate, deathRate);
population = calculatePop(sPopulation, birthRate, deathRate);
outputTable(sPopulation, birthRate, deathRate, time, population);
//calculate birthRate
birthRate = birthRate/100;
//calculate deathRate
deathRate = deathRate/100;
//calculate growth rate
growthRate = (birthRate-deathRate);
//check for positive values to decide for double
numRaw = yearsToDouble(growthRate);
if ( numRaw != 0){
cout << "Population will be double in "<< numRaw << "years ";
}
//check for negative values to decide for zero
numRaw = yearsToZero(sPopulation,growthRate);
if ( numRaw != 0){
cout << "Population will be zero in "<< numRaw << "years ";
}
//continue program loop
cout<< "Do you want to continue (Y/N)? ";
cin >> ans;
}
while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));
return 0;
}
// start getInput function
void getInput(int &sPopulation, double &birthRate, double &deathRate, double &time)
{
cout << " ";
cout << "Enter the inital population: ";
cin >> sPopulation;
cout << " ";
cout << " Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
cout << " ";
cout << " Enter the annual death rate (as % of current population): ";
cin >> deathRate;
cout << " ";
cout << " Enter the number of years of population changes: ";
cin >> time;
cout << " ";
}// end getInput function
// start calculatePop function, calcualats the formula for finding projected population
double calculatePop(int sPopulation, double birthRate, double deathRate)
{
int population = 0;
// calculate population for output
population += (sPopulation + ( birthRate * sPopulation) - (deathRate * sPopulation));
return population;
}
//If Growth Rate is positive calculate years to double
//Start yearsToDouble
double yearsToDouble( double growthRate)
{
if (growthRate > 0)
{
int num = log(2)/log(1+growthRate);
return num;
}
else
return 0;
}
//If Growth Rate is negative calculate years to become ZERO
//Start yeartsToZero
double yearsToZero( int sPopulation,double growthRate)
{
if (growthRate < 0)
{
int num = log(1/sPopulation)/log(1+growthRate);
return num;
}
else
return 0;
}
//Print the output in the table format as we want
//start outputTable function
void outputTable( int sPopulation, double birthRate, double deathRate, double time, int population)
{
// display population table
cout << " ";
//decalre local variable
int i = 0;
//Print in table format
for (int i = 1; i <= time; i+= population)
{
double population = endPopulationFunction(sPopulatiion, birthRate, deathRate);
cout << " Year Population ";
cout << "------ ----------- ";
cout << i << " " << population << " ";
sPopulatiion = population;
}
}
////////////////////////////////////end of program//////////////////
But I keep getting an error saying in line 217 - double population = endPopulationFunction(sPopulatiion, birthRate, deathRate); sPopulation is not declared in the scope.How can I fix this? and also does this program look correct besides the error?
Explanation / Answer
Note:
Program:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//declare function prototype
void getInput(int &, double &, double &, double &); // get starting population, birth rate, death rate, and time in years data function
void endPopulationFunction(int , double , double );
double calculatePop(int, double, double);//calculate the population
double population(double);
double yearsToDouble(double); //+ve growth rate
double yearsToZero(int , double);//-ve growth rate
double sPopulation(int);
void outputTable(int, double, double, double, int); // calculate and loop for the projected population data and prints to screen function
// start main function
int main()
{
//Keep asking user for input until Y
char ans;
do
{
//declare and initalize variables
int sPopulation=0;
double birthRate=0;
double deathRate=0;
double population=0;
double time = 0;
double growthRate=0;
int numRaw = 0;
//display title and purpose
cout << " ";
cout << " Welcome to the Population Monitor Program!
";
cout << " Projects population based on starting
population, birth rate, ";
cout << " death rate, and time in years. Displays a
table to show growth ";
cout << " or decline of the population. ";
// call functions
getInput(sPopulation, birthRate, deathRate, time);
endPopulationFunction(sPopulation, birthRate,deathRate);
population = calculatePop(sPopulation, birthRate, deathRate);
outputTable(sPopulation, birthRate, deathRate, time, population);
//calculate birthRate
birthRate = birthRate/100;
//calculate deathRate
deathRate = deathRate/100;
//calculate growth rate
growthRate = (birthRate-deathRate);
//check for positive values to decide for double
numRaw = yearsToDouble(growthRate);
if ( numRaw != 0)
{
cout << "Population will be double in "<< numRaw << "years ";
}
//check for negative values to decide for zero
numRaw = yearsToZero(sPopulation,growthRate);
if ( numRaw != 0)
{
cout << "Population will be zero in "<< numRaw << "years ";
}
//continue program loop
cout<< "Do you want to continue (Y/N)? ";
cin >> ans;
}
while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));
return 0;
}
void endPopulationFunction(int sPopulation, double birthRate, double deathRate)
{
int population;
population += (sPopulation + ( birthRate * sPopulation) - (deathRate * sPopulation));
}
// start getInput function
void getInput(int &sPopulation, double &birthRate, double &deathRate, double &time)
{
cout << " ";
cout << "Enter the inital population: ";
cin >> sPopulation;
cout << " ";
cout << " Enter the annual birth rate (as % of current
population): ";
cin >> birthRate;
cout << " ";
cout << " Enter the annual death rate (as % of current
population): ";
cin >> deathRate;
cout << " ";
cout << " Enter the number of years of population
changes: ";
cin >> time;
cout << " ";
}// end getInput function
// start calculatePop function, calcualats the formula for finding projected population
double calculatePop(int sPopulation, double birthRate, double deathRate)
{
int population = 0;
// calculate population for output
population += (sPopulation + ( birthRate * sPopulation) - (deathRate * sPopulation));
return population;
}
//If Growth Rate is positive calculate years to double
//Start yearsToDouble
double yearsToDouble( double growthRate)
{
if (growthRate > 0)
{
int num = log(2)/log(1+growthRate);
return num;
}
else
return 0;
}
//If Growth Rate is negative calculate years to become ZERO
//Start yeartsToZero
double yearsToZero( int sPopulation,double growthRate)
{
if (growthRate < 0)
{
int num = log(1/sPopulation)/log(1+growthRate);
return num;
}
else
return 0;
}
//Print the output in the table format as we want
//start outputTable function
void outputTable( int sPopulation, double birthRate, double deathRate, double time, int population)
{
// display population table
cout << " ";
//decalre local variable
int i = 0;
//Print in table format
for (int i = 1; i <= time; i+= population)
{
cout << " Year Population ";
cout << "------ ----------- ";
cout << i << " " << sPopulation << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.