C++ question

Recommended Videos

gamebrain89

New member
May 29, 2008
544
0
0
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!
 

Avatar Roku

New member
Jul 9, 2008
6,169
0
0
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.
 

gamebrain89

New member
May 29, 2008
544
0
0
orannis62 said:
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!
 

Teleios

New member
Aug 5, 2003
212
0
0
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. :)
 
Feb 13, 2008
19,429
0
0
gamebrain89 said:
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.
 

gamebrain89

New member
May 29, 2008
544
0
0
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.
 

Calobi

New member
Dec 29, 2007
1,504
0
0
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

New member
Dec 29, 2007
1,504
0
0
gamebrain89 said:
Calobi said:
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.
 

gamebrain89

New member
May 29, 2008
544
0
0
Ok, this might be easier if I include all the code. So here it is.

// Population
#include
#include
using namespace std;

double population(double pop, double birthRate, double deathRate);
void printPopulations(
double startPop, double birthRate, double deathRate, int numYears);


// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------


// --------------------------------
// --------- END USER CODE --------
// --------------------------------


int main()
{
double startPop, // To hold the starting population.
birthRate, // To hold the birth rate.
deathRate; // To hold the death rate.
int numYears; // To hold the number of years to track population changes.

// Input and validate starting population
cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startPop;
while (startPop < 2.0)
{
cout << "Starting population must be 2 or more. Please re-enter: ";
cin >> startPop;
}

// Input and validate annual birth and death rates
cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate percent cannot be negative. Please re-enter: ";
cin >> birthRate;
}

birthRate = birthRate / 100; // Convert from % to decimal.

cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate percent cannot be negative. Please re-enter: ";
cin >> deathRate;
}

deathRate = deathRate / 100; // Convert from % to decimal.

cout << "For how many years do you wish to view population changes? ";
cin >> numYears;
while (numYears < 1)
{
cout << "Years must be one or more. Please re-enter: ";
cin >> numYears;
}

printPopulations(startPop, birthRate, deathRate, numYears);
return 0;
} // End of main function.

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

Calobi

New member
Dec 29, 2007
1,504
0
0
#include [iostream]
#include [iomanip]

using namespace std;

double population(double pop, double birthRate, double deathRate){

double n = 0 ;

n = pop + (birthRate*pop) - (deathRate*pop);

return n;

}

void printPopulations(double startPop, double birthRate, double deathRate, int numYears){

for ( int i = 0 ; i < numYears ; i++ ){
startPop = population ( startPop , double birthRate, double deathRate, int numYears );
cout << "For year " << i+1 << " the population is " << startPop << endl ;
}

}

int main(int argc, char *argv[]){

double startPop, birthRate, deathRate;
int numYears;

cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startPop;
while (startPop < 2.0)
{
cout << "Starting population must be 2 or more. Please re-enter: ";
cin >> startPop;
}

cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate percent cannot be negative. Please re-enter: ";
cin >> birthRate;
}

birthRate = birthRate / 100;

cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate percent cannot be negative. Please re-enter: ";
cin >> deathRate;
}

deathRate = deathRate / 100;

cout << "For how many years do you wish to view population changes? ";
cin >> numYears;
while (numYears < 1)
{
cout << "Years must be one or more. Please re-enter: ";
cin >> numYears;
}

printPopulations(startPop, birthRate, deathRate, numYears);
return 0;
}

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.
 

gamebrain89

New member
May 29, 2008
544
0
0
Calobi said:
#include [iostream]
#include [iomanip]

using namespace std;

double population(double pop, double birthRate, double deathRate){

double n = 0 ;

n = pop + (birthRate*pop) - (deathRate*pop);

return n;

}

void printPopulations(double startPop, double birthRate, double deathRate, int numYears){

for ( int i = 0 ; i < numYears ; i++ ){
startPop = population ( startPop , double birthRate, double deathRate, int numYears );
cout << "For year " << i+1 << " the population is " << startPop << endl ;
}

}

int main(int argc, char *argv[]){

double startPop, birthRate, deathRate;
int numYears;

cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startPop;
while (startPop < 2.0)
{
cout << "Starting population must be 2 or more. Please re-enter: ";
cin >> startPop;
}

cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate percent cannot be negative. Please re-enter: ";
cin >> birthRate;
}

birthRate = birthRate / 100;

cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate percent cannot be negative. Please re-enter: ";
cin >> deathRate;
}

deathRate = deathRate / 100;

cout << "For how many years do you wish to view population changes? ";
cin >> numYears;
while (numYears < 1)
{
cout << "Years must be one or more. Please re-enter: ";
cin >> numYears;
}

printPopulations(startPop, birthRate, deathRate, numYears);
return 0;
}

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.
 

Calobi

New member
Dec 29, 2007
1,504
0
0
gamebrain89 said:
Calobi said:
#include [iostream]
#include [iomanip]

using namespace std;

double population(double pop, double birthRate, double deathRate){

double n = 0 ;

n = pop + (birthRate*pop) - (deathRate*pop);

return n;

}

void printPopulations(double startPop, double birthRate, double deathRate, int numYears){

for ( int i = 0 ; i < numYears ; i++ ){
startPop = population ( startPop , double birthRate, double deathRate, int numYears );
cout << "For year " << i+1 << " the population is " << startPop << endl ;
}

}

int main(int argc, char *argv[]){

double startPop, birthRate, deathRate;
int numYears;

cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startPop;
while (startPop < 2.0)
{
cout << "Starting population must be 2 or more. Please re-enter: ";
cin >> startPop;
}

cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate percent cannot be negative. Please re-enter: ";
cin >> birthRate;
}

birthRate = birthRate / 100;

cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate percent cannot be negative. Please re-enter: ";
cin >> deathRate;
}

deathRate = deathRate / 100;

cout << "For how many years do you wish to view population changes? ";
cin >> numYears;
while (numYears < 1)
{
cout << "Years must be one or more. Please re-enter: ";
cin >> numYears;
}

printPopulations(startPop, birthRate, deathRate, numYears);
return 0;
}

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.
 

Avatar Roku

New member
Jul 9, 2008
6,169
0
0
Iffypop said:
orannis62 said:
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.
 

Rensa

New member
Nov 4, 2008
21
0
0
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 :)