Topic Index
C++ question

Username:Password:
Log In

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!

I program in Java, but maybe I can help. Let me get a look at it. I'll post again when/if I figure out your problem.

orannis62:
I program in Java, but maybe I can help. Let me get a look at it. I'll post again when/if I figure out your problem.

Ok, thanks man, I appreciate it!

cout<<year<<"\t\t"<<double population(startPop,birthRate,deathRate)<<endl;

should be

cout<<year<<"\t\t"<< population(startPop,birthRate,deathRate)<<endl;

You don't need the type declaration when you actually call your functions.

{for(year=1; year <= numYears; year++,newpop=startPop)
{
cout<<year<<"\t\t"<<double population(startPop,birthRate,deathRate)<<endl;
cout<<grandTotal;

}

I'd change this to something like this:

{for(year=1; year <= numYears; year++)
{
newpop = startpop;
cout<<year<<"\t\t"<<population(startPop,birthRate,deathRate)<<endl;
cout<<grandTotal;
cout<<endl;
}

Give those a shot and see if they help. :)

Wow, I just realized how different C++ and Java are. I won't be any help, sorry.

gamebrain89:

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.

Calobi:

int p = argv[1] ;
int b = argv[2] ;
int d = argv[3] ;
int years = argv[4] ;

These lines give me a "invalid conversion from char* to int error

gamebrain89:

Calobi:

int p = argv[1] ;
int b = argv[2] ;
int d = argv[3] ;
int years = argv[4] ;

These lines give me a "invalid conversion from char* to int error

Yea, you'd need to use atoi to convert them from pointers to chars (char *)to ints. Like I said, mine really just psuedo-code.

Psst, check your messages.

Ok, this might be easier if I include all the code. So here it is.

calobi, I am still new to this stuff, so I am not really understanding you. sorry.

This might work, not sure cause I'm in Windows not Linux. Worth a shot, and it should be close enough to working for you to get the right answer.

Calobi:

This might work, not sure cause I'm in Windows not Linux. Worth a shot, and it should be close enough to working for you to get the right answer.

Ok, Thanks alot Calobi, you have been a big help. now I am just getting parse errors on line 29, I should be able to figure it out now, hopefully.

gamebrain89:

Calobi:

This might work, not sure cause I'm in Windows not Linux. Worth a shot, and it should be close enough to working for you to get the right answer.

Ok, Thanks alot Calobi, you have been a big help. know I am just getting parse errors on line 29, I should be able to figure it out know.

It's been my pleasure. I'm actually taking a C++ class right now, so it's helped me as well. Plus, I'm banking on karma to help me later.

orannis62:
Wow, I just realized how different C++ and Java are. I won't be any help, sorry.

Java is kind of like C++--

Iffypop:

orannis62:
Wow, I just realized how different C++ and Java are. I won't be any help, sorry.

Java is kind of like C++--

Not enough that I could help with this sort of problem. It seems to be a problem of formats, not calculations.

...I was calling Java a bad language ^^

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 wouldn't say usually, as you just said it's a matter of style ;P

 
Topic Index

Reply to Thread

Log in or Register to Comment
Have an account? Login below Login With Facebook
or
Username:  
Password:  
  
Not registered? To sign up for an account with The Escapist, Register With Facebook
or
Registered for a free account here
Forum Jump: