Pulitzer Laureate Posts: 719 Joined: 29 May 2008 | |
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 |
Ok, thanks man, I appreciate it! |
Developer Emeritus Posts: 1572 Joined: 5 Aug 2003 |
should be cout<<year<<"\t\t"<< population(startPop,birthRate,deathRate)<<endl; You don't need the type declaration when you actually call your functions.
I'd change this to something like this: {for(year=1; year <= numYears; year++) 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 |
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 ) { int main ( int argc, char *argv[]) { Something like that should work. I'm assuming you get all the arguments from command line at the time of execution. 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 |
These lines give me a "invalid conversion from char* to int error |
Gone Gonzo Posts: 1781 Joined: 29 Dec 2007 |
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 |
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 |
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 |
Java is kind of like C++-- |
On the Record Posts: 5743 Joined: 9 Jul 2008 |
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 |
|
|
Not registered? Sign up for a free account! |
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!