- #1
sandy.bridge
- 798
- 1
Simple Recursive Function-- returning "nan"
I have a program that is supposed to simulate a growth population. The details are not of much importance. It is a rather simple program, however, I am having problems with the program returning the final value that it has stored in X. It returns "nan", which means not a number, however, I am not seeing why. Perhaps one of you do? Thanks in advance!
Here is an example of the console:
I am wanting the final population to output Population when Time is 0.
Homework Statement
I have a program that is supposed to simulate a growth population. The details are not of much importance. It is a rather simple program, however, I am having problems with the program returning the final value that it has stored in X. It returns "nan", which means not a number, however, I am not seeing why. Perhaps one of you do? Thanks in advance!
Code:
#include <iostream>
using namespace std;
double growth(int timeSteps, double r, double X)
{
if(timeSteps >= 0)
{
if (X < 0)
{
X = 0;
}
else if (X > 1)
{
X = 1;
} cout << timeSteps << " " << X << endl;
X = r*X*(1-X);
timeSteps = timeSteps - 1;
growth(timeSteps, r, X);
if(timeSteps==0){return X;}
}
}int main() {
cout << "Specify model parameters (Time limit, Growth, Initial Population): ";
double X, r;
int timeSteps;
cin >> timeSteps >> r >> X;
cout << "Time " << "Population" << endl;
double R = growth(timeSteps, r, X);
cout << "The final population was: " << R; return 0;
}
Here is an example of the console:
Code:
Specify model parameters (Time limit, Growth, Initial Population): 5 1 0.5
Time Population
5 0.5
4 0.25
3 0.1875
2 0.152344
1 0.129135
0 0.112459
The final population was: nan
I am wanting the final population to output Population when Time is 0.
Last edited: