Hi can anyone tell me why my code is getting a stack overflow? also any other tips would be nice, im trying to make a program to go with the logistic function for population control
#include<iostream>
#include<cstdlib>
#include<cmath>
usingnamespace std;
//void ask(int r, int n);
int logistic(int n, int r);
int main()
{
// the variables that will input the values
// n is the value for the number of generations
// r is the value for the combined rate for reproduction and starving
int n, r, x;
cout << "What is the value of x(0)? " << endl;
cin >> x;
cout << "What is the value for r? " << endl;
cin >> r;
cout << "What is the value for n? " << endl;
cin >> n;
cout << "The initial population is " << x;
cout << logistic(n, r);
return 0;
}
/*void ask(int r, int n)
{
cout << "What is the value for r? " << endl;
cin >> r;
cout << "What is the value for n? " << endl;
cin >> n;
}*/
int logistic(int n, int r)
{
if (logistic(n, r) <= 0)
{
cout << "The population will never recover." << endl;
return 0;
}
elseif (logistic(n, r) > 1)
{
cout << "The population will exceed the population capacity." << endl;
return 0;
}
else
{
return r * (logistic(n, r)*(1-logistic(n, r)));
}
}
In your recursive function logistic, you do not have a base case (termination case). The function will keep calling itself indefinitely until stack overflow.
#include<iostream>
#include<cstdlib>
#include<cmath>
usingnamespace std;
// rate of increase is double and initial population is needed as a parameter.
// rate = 0 to 1 (fractional increase in population every year)
int logistic(int generations, double rate, int initial_pop);
int main()
{
// the variables that will input the values
// n is the value for the number of generations
// r is the value for the combined rate for reproduction and starving
int n, x;
double r;
cout << "What is the value of x(0)? " << endl;
cin >> x;
cout << "What is the value for r? " << endl;
cin >> r;
cout << "What is the value for n? " << endl;
cin >> n;
cout << "The initial population is " << x;
cout << logistic(n, r, x);
return 0;
}
int logistic(int generations, double rate, int initial_pop)
{
if ( generations == 0 )
return initial_pop;
return logistic( generations-1, rate, initial_pop*(1+rate) );
}
The only problem with this is that with the function that I am using, if x(n) is less than or equal to 0 or greater than 1, I have to end the recursion. Maybe I did that wrong in my initial statement. Does your change take this into account?
I must be doing something horribly wrong, im trying to check whether it is above 1 or equal to or below zero all the time, not just for the initial population, but that seems to be causing my stack overflow, any help?
1 2 3 4 5 6 7 8 9 10
int logistic(int generations, double rate, int initial_pop)
{
if (logistic(generations, rate, initial_pop) <= 0 && logistic(generations, rate, initial_pop) > 1 )
return 0; // or whatever.
if ( generations == 0 )
return initial_pop;
return logistic( generations-1, rate, initial_pop*(1+rate) );
}
im not sure how to explain, I just tried your initial post, it seems to be working, sorry for the trouble, heres what im reading out of in my book if it can help you understand better
n for a given value of n. If the value for xn becomes > 1, then you can assume that the population will exceed the population capacity and should terminate subsequent computations. Likewise if xn < 0, you can opt to terminate early since the population will never recover
This is for Peirre Francois Verhulst's logistic algorithm
It will help to understand the problem if you post full text from the book.
However, more than that I would like you to understand how recursion works in general.
1 2 3 4 5 6 7 8
int myRecursiveFactorial( unsignedint n ) {
if ( n == 0 ) // base case when the recursion should stop and start backwards.
return 1;
return n*myRecursiveFactorial( n-1 );
}
Note that at each recursion, we reduce the value of n so that after some iterations it is guaranteed to reach the base case.