I need some help with a C++ problem , and thought some of you guys might know something about it. I have to write two functions. one function that calculates the size of the population for a year. The formula is:
N = P + BP - DP Where N is the new population size, P is the previous population size, B is the birth rate, and D is the death rate.
And another is The printPopulations function should use a loop to compute and output the population for each of the numYears years. Each iteration of the loop should call the population function to compute the change in population for the year. I only have to write the functions, the rest of the program is already written for me. here is the code I have written. I am pretty sure its not right.
double population (double pop, double birthRate, double deathRate) {double newpop=pop+(pop*birthRate)-(pop*deathRate);
double population (double pop, double birthRate, double deathRate) {double newpop=pop+(pop*birthRate)-(pop*deathRate);
return newpop;}
void printPopulations(double startPop, double birthRate, double deathRate, int numYears) { double year; double grandTotal; double newpop; {for(year=1; year <= numYears; year++,newpop=startPop) Problem 1: Newpop isn't defined at the start of the loop, so will be null { cout<<year<<"\t\t"<<double population(startPop,birthRate,deathRate)<<endl; Problem 2: You're calling with startPop instead of newpop) cout<<grandTotal; Problem 3: grandtotal is never given a value so will be null
} }
At least I think so...been some time since I did C++, will get back to you once I've re-read my notes.
They way I understand it is that the print populations function should call the double population function, then take the number it returns, display it, then use that number as the new startPop, and go through the double population function again as many times as the user tells it to, which is the numYears. I don't think I have my print population function correct. As far as I can tell, my double population function is correct, but I don't know how to get the print population function correct. I understand how it should work, but getting the code is giving me problems.
double newpop ( double p , double b , double d ) { double n ; n = p + bp - dp; return n ; }
int main ( int argc, char *argv[]) { double p = argv[1] ; double b = argv[2]/100 ; double d = argv[3]/100 ; double years = argv[4] ; for ( double i = 0 ; i < years ; i++ ) { p = newpop ( p , b , d ) ; } cout << p << endl ; return 0 ; }
Something like that should work. I'm assuming you get all the arguments from command line at the time of execution. Also, I used ints rather than doubles, just because I like ints better. There may be a few bugs in there, just sort of thought it out and wrote what seemed like it should work.
Edit: I see why you need doubles. You need to divide by 100 the birth and death rates, so ints would always truncate down, thus causing the program to fail. Clever.
This is really just a style thing, but counting variables like year are usually declared right there inside the for loop definiton. So for example, for(int i = 1; i < 5; i++). That way the counting variable's scope ends when the loop's does :)
I need some help with a C++ problem , and thought some of you guys might know something about it.
I have to write two functions. one function that calculates the size of the population for a year. The formula is:
N = P + BP - DP
Where N is the new population size, P is the previous population size, B is the birth rate, and D is the death rate.
And another is The printPopulations function should use a loop to compute and output the population for each of the numYears years. Each iteration of the loop should call the population function to compute the change in population for the year. I only have to write the functions, the rest of the program is already written for me. here is the code I have written. I am pretty sure its not right.
double population (double pop, double birthRate, double deathRate)
{double
newpop=pop+(pop*birthRate)-(pop*deathRate);
return newpop;}
void printPopulations(double startPop, double birthRate, double deathRate, int numYears)
{ double year;
double grandTotal;
double newpop;
{for(year=1; year <= numYears; year++,newpop=startPop)
{
cout<<year<<"\t\t"<<double population(startPop,birthRate,deathRate)<<endl;
cout<<grandTotal;
}
}
Thanks for your help!