0

I have to write a simple program as follows: "Given a non-negative integer n, find the nth Fibonacci number using recursion". I think what this means is that, for any value entered by the user, I have to get the Fibonacci number. For example, if the user entered 4, I would have to get the 4th value in the list of Fibonacci numbers (which is 2). Below is what I have written, but there is something wrong with my recursion as it crashes when I run it. Appreciate any help...

int userValue;
int fibo;
int fib(int n);
int fibValue;


int main() {
    cout << "Please provide your value" << endl;
    cin >> userValue;

    while (userValue < 0) {
        cout << "Fibonacci numbers only start at 0, please try again: " << endl;
        cin >> userValue;
    }

    if (userValue == 1 || userValue == 0) {
        cout << "Fibonacci result is: " << userValue << endl;
        return 0;
    }
    else {
        fib(userValue);
        cout << "Fibonacci result is: " << fibValue << endl;
        //return 0;
    }
}

int fib(int n)
{
    fibValue = fib(n - 1) + fib(n - 2);
    return fibValue;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

3 Answers3

2

The problem lies in fib method.There is no termination condition provided. So, The recursion will happen in a loop without terminating.

First, try to debug any problem by giving multiple inputs and you will understand where the problem lies.

In your case,

For suppose n=3,

the trace would be like this

fib(3) -> which further invokes fib(2) and fib(1)

fib(2) -> which further invokes fib(1) and fib(0)

now since there is no termination condition

fib(0) will further invoke fib(-1) and fib(-2)

since fib of negative value does not exists termination condition should be provided so that recursion would stop and return the result.

For fibonacci number, termination condition would be like:

 if(n == 0){
  return 0;
 }else if (n == 1){
  return 1;
 }

Few reference

https://blog.hartleybrody.com/debugging-code-beginner/

https://www.codementor.io/mattgoldspink/how-to-debug-code-efficiently-and-effectively-du107u9jh%60

Hope this helps. Thanks.

samk
  • 410
  • 5
  • 13
  • Due to `if(n == 0 || n==1){ return 0; }` - anything said function would return, is `0`, since no matter how many times you would add `0` to `0`, due to `fib(n - 1) + fib(n - 2);` - it will never become greater than `0`. – Algirdas Preidžius Nov 26 '19 at 14:58
  • ```if(n == 0 || n==1){ return 0; }``` is just an example to show termination condition. Anyway I edited my answer specific to fibonacci!.Thanks. – samk Nov 26 '19 at 15:07
  • That may be, but it still, might look confusing for the asker. – Algirdas Preidžius Nov 26 '19 at 15:15
  • yes @AlgirdasPreidžius, I realised. Thanks for the comment. – samk Nov 26 '19 at 15:22
0

Your recursion's stop condition is outside of it, in the following if:

if (userValue == 1 || userValue == 0) {...}

But it should instead be in the fib function.

What you currently have is an endless recursion which is the shortest path to a stack overflow.

mlntdrv
  • 137
  • 1
  • 8
-1

Recursion in the above code never stops, it stops only when stack is full resulting in run-time termination of program. For you program:

int fib(int n){
    if(n<=1)
        return n;
    else
        return fib(n-1)+fib(n-2);
}
Shrey
  • 98
  • 1
  • 12
  • What is the 3rd fibonacci number, according to your function? Since, based on it, it seems that 1st, 2nd, and 3rd numbers, are equal to 1. That's not what fibonacci sequence is: https://en.wikipedia.org/wiki/Fibonacci_number – Algirdas Preidžius Nov 26 '19 at 14:56
  • yes, for ```n == 3``` the fibonnaci number would be 1 according to your code. – samk Nov 26 '19 at 15:11
  • Well since Fibonacci can start from 0, making it 0,1,1 yes third number is 1, but for first and second he is already checking before calling the recursion. – Shrey Nov 27 '19 at 09:13