|
-
July 23rd, 2009, 06:45 PM
#1
could someone help me fix this?
the initial population is not working for some reason. could anyone help me fix it as this is a dire situation for finishing this program assignment. I would appreciate it.
Code:
#include<iostream>
#include<cmath>
//Source of Verhulst Formula: http://www.unc.edu/depts/cmse/math/Verhulst.html
using namespace std;
double population, growth_Percent, growth_rate, loss_Percent, h, carry_capacity, year, temp; //Declaration of global varaibles
double expectedpopulation (double years); //prototype expectedpopulation
int main()
{
cout << "The verhulst formula program" << endl;
cout << "Please provide the initial population : ";
while( !( cin >> population )|| population < 0 )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');//Professor J. Epstein did this part when I was observing with Devin
cout << "Please provide the initial population : ";
}
cout << "Please provide the population growth rate percentage : ";
while( !( cin >> growth_rate )|| growth_rate < 0 )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Please provide the population growth rate percentage : ";
growth_rate = growth_rate/100; // percent converted to a decimal
}
cout << "Please provide the population loss rate percentage : ";
while(!(cin >> h) ||h < 0 )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Please provide the population loss rate percentage : ";
h = h/100; //percent converted to a decimal
}
cout << "Please provide the population capacity : ";
while(!(cin >> carry_capacity) || carry_capacity < 0 )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Please provide the population capacity : ";
}
cout << "Please provide the number of year(s) after the initial population to predict the expected population : ";
while(!(cin >> year )|| year < 0 )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Please provide the number of year(s) after the initial population to predict the expected population : ";
}
cout << "The expected population " << year << " year(s) from the initial population is " << expectedpopulation(year) << endl;
system("pause");
return 0;
}
double expectedpopulation (double years)// this is the recursion function
{
if (years == 0)
return population;
else
double temp = expectedpopulation(years-1);
return (1 + growth_rate - h)*temp - growth_rate * pow( temp, 2 ) / carry_capacity;
}
-
July 23rd, 2009, 08:47 PM
#2
Re: could someone help me fix this?
This is what you need.
Code:
double expectedpopulation (double years)// this is the recursion function
{
double temp;
if (years == 0)
{
return population;
}
else
{
temp = expectedpopulation(years-1);
}
return (1 + growth_rate - h)*temp - growth_rate * pow( temp, 2 ) / carry_capacity;
}
The braces around the return population, and temp=expectedpopulation(years-1); are not necessary but make the code structure more obvious. Declaring the double temp in the else clause without the braces caused the problem. I don't know why the compiler didn't catch it.
-
July 23rd, 2009, 09:03 PM
#3
Re: could someone help me fix this?
Oh, you have a temp declared at the top of the file, so the double temp declaration in the else clause has no effect in the calculation. It is created, assigned, and discarded in the else clause and the temp from the top is used in the calculation. You should get rid of the temp at the top of the file because its not used anywhere.
-
July 24th, 2009, 03:08 AM
#4
Re: could someone help me fix this?
 Originally Posted by BertAndErnie
Oh, you have a temp declared at the top of the file, so the double temp declaration in the else clause has no effect in the calculation. It is created, assigned, and discarded in the else clause and the temp from the top is used in the calculation. You should get rid of the temp at the top of the file because its not used anywhere.
That is only true because the OP is lacking braces and so the temp went out of scope. It is not true for the code you supplied, nor would not be true if the OP added the following braces:
Code:
double expectedpopulation (double years)// this is the recursion function
{
if (years == 0)
return population;
else
{
double temp = expectedpopulation(years-1);
return (1 + growth_rate - h)*temp - growth_rate * pow( temp, 2 ) / carry_capacity;
}
}
In addition, in my opinion a single return point should be preferred where appropriate:
Code:
double expectedpopulation (double years)// this is the recursion function
{
double result(population);
if (years != 0)
{
double temp = expectedpopulation(years-1);
result = (1 + growth_rate - h)*temp - growth_rate * pow( temp, 2 ) / carry_capacity;
}
return result;
}
Last edited by PredicateNormative; July 24th, 2009 at 03:12 AM.
Reason: modified code and comment.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|