Topic Index
C++ question

Username:Password:
Log In
Pulitzer Laureate
Posts: 719
Joined: 29 May 2008

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!

On the Record
Posts: 5743
Joined: 9 Jul 2008

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.

Pulitzer Laureate
Posts: 719
Joined: 29 May 2008

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!

Developer Emeritus
Posts: 1572
Joined: 5 Aug 2003

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. :)

On the Record
Posts: 5743
Joined: 9 Jul 2008

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

Time Lord
Posts: 9978
Joined: 13 Feb 2008

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.

Pulitzer Laureate
Posts: 719
Joined: 29 May 2008

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.

Gone Gonzo
Posts: 1781
Joined: 29 Dec 2007

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.

Pulitzer Laureate
Posts: 719
Joined: 29 May 2008

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

Gone Gonzo
Posts: 1781
Joined: 29 Dec 2007

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.

Pulitzer Laureate
Posts: 719
Joined: 29 May 2008

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.

Gone Gonzo
Posts: 1781
Joined: 29 Dec 2007

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.

Pulitzer Laureate
Posts: 719
Joined: 29 May 2008

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.

Gone Gonzo
Posts: 1781
Joined: 29 Dec 2007

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.

Copy Clerk
Posts: 65
Joined: 2 Apr 2008

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

Java is kind of like C++--

On the Record
Posts: 5743
Joined: 9 Jul 2008

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.

Copy Clerk
Posts: 65
Joined: 2 Apr 2008

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

Paperboy
Posts: 19
Joined: 4 Nov 2008

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 :)

Copy Clerk
Posts: 65
Joined: 2 Apr 2008

I wouldn't say usually, as you just said it's a matter of style ;P

 
Topic Index

Reply to Thread

You must be logged in to post.
Username:  
Password:  
  

Not registered? Sign up for a free account!

Forum Jump: