I searched the forums but I didn't find any useful information. I am just trying to write a simple recursive solution to do F(n). It seems simple to me but I am probably not thinking about it correctly. My program has no errors, but it only returns 55 and then exits.

#include <iostream>

using namespace std;

int F(int);

int main()
{
    int fibonacci_input;
    cout << "Enter a positive integer to be Fibonacci'ed: ";
    cin >> fibonacci_input;
    cout << endl << F(fibonacci_input) << endl;
    return 0;
} //end main

int F(int n)
{
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return F(n-1)+F(n-2);
} //end F()

Recommended Answers

All 4 Replies

Recursion means you call the function until you get to a specific condition. In this case, you might want to call the function again with n-1. Then when you get to the n==1 you start returning and add all the values returned to get the value.

I searched the forums but I didn't find any useful information. I am just trying to write a simple recursive solution to do F(n). It seems simple to me but I am probably not thinking about it correctly. My program has no errors, but it only returns 55 and then exits.

#include <iostream>

using namespace std;

int F(int);

int main()
{
    int fibonacci_input;
    cout << "Enter a positive integer to be Fibonacci'ed: ";
    cin >> fibonacci_input;
    cout << endl << F(fibonacci_input) << endl;
    return 0;
} //end main

int F(int n)
{
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return F(n-1)+F(n-2);
} //end F()

Okay Brother here is what your doing :
you input a value from the user and pass it as an argument to the function F(); right? now lets see what the function does if I pass 5.
Passing 10 executes the else statement i.e. return the sum of return value of F(4) and F(3) these in turn retruns the previous returns value. So in turn it returns :
0+1+1+2+3 and hence 5 is displayed.
But Actually you want to display all the values that are been encountered in the series.
Hence, you need to cout the F(0) then F(1) then F(2) then F(3) then F(4) .....till F(n)
Hence you will have to use the loop. Hence the revised code is :

#include <iostream.h>

//using namespace std;

int F(int);

int main()
{
    int fibonacci_input;
    cout << "Enter a positive integer to be Fibonacci'ed: ";
    cin >> fibonacci_input;
    for(int i=0;i<=fibonacci_input;i++)
    cout << endl << F(i) << endl;
    return 0;
} //end main

int F(int n)
{
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return F(n-1)+F(n-2);
} //end F()

Try it out

> #include <iostream.h> You should not use iostream.h, IOSTREAM is the standard header file for C++ I/O operations. and also de-comment your using namespace std; becasue it's necessary.

Also, the std::cout statement should be inside the function F(int) rather than main() because it is supposed to display the result recursively. This is just iteration. there is no need for the for loop.

> #include <iostream.h> You should not use iostream.h, IOSTREAM is the standard header file for C++ I/O operations. and also de-comment your using namespace std; becasue it's necessary.

Also, the std::cout statement should be inside the function F(int) rather than main() because it is supposed to display the result recursively. This is just iteration. there is no need for the for loop.

yeah. that's what I wanted to do. I didn't think I need to the loop. But I'm not sure exactly how to implement the cout statement inside F(n)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.