Runtime stack overflow

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

thank you in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace 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;
	}
	else if (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.
any tips on how I would implement that?
That depends on what the function logistic does. It is unclear. Does it output the population level after n generations?
yes, at least thats what its meant to do
Try the following change:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace 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) );
}
Last edited on
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?
Last edited on
How can the initial population be between 0 and 1. Anyways, it can be easily incorporated in the program as:

1
2
3
4
5
6
7
8
9
10
int logistic(int generations, double rate, int initial_pop)
{
    if ( initial_pop <= 0 or initial_pop > 1 )
      return 0; // or whatever.

    if ( generations == 0 )
      return initial_pop;

    return logistic( generations-1, rate, initial_pop*(1+rate) );
}
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) );
}
I have still not understood what you want to do.

Please give an example of input and output data.
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( unsigned int 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.
Your code
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) );
}

May as well be
1
2
3
4
5
int logistic(int generations, double rate, int initial_pop)
{
    logistic(generations, rate, initial_pop);
    // This will never be reached
    return
}

You need to do some check before recursing.
Topic archived. No new replies allowed.