Thread: Fibonacci series using a recursive function

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Fibonacci series using a recursive function

    Hello all this my first post, so bear with me if I don't do something correctly.

    This is my question: below I have the format for my recursive function but am really unsure of what I need to put in the Underscore areas ?

    If anyone has any ideas, I have never done a Fibonacci series of numbers before, so I'm lost as to how it has to be set up.

    #include <iostream>
    using namespace std;

    unsigned long fib(int nth);

    void main()
    {
    int nth=0;

    cout << "Enter an integer from 1 to 30: ";

    while (!(cin >> nth))
    {
    cin.clear();
    while (cin.get() != '\n')
    continue;
    cout << " Please! Enter a number: ";
    }
    if((nth < 1) || (nth > 30))
    cout << "\nSorry. "
    << "Number isn't in the right range.";
    else
    {
    unsigned long fibValue;
    fib(int fibValue);

    cout << "\nThe value of number " << nth;
    cout << " in the series is " << fibValue
    << ".\n";
    }
    }

    // Recursive function
    unsigned long fib(int nth)
    {
    if(______)
    return(___);
    else
    return(_________ + ____________);
    }

  2. #2
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    Well since you can find an implementation of the recursive fibonacci function in any decent intro to C++ book, i'll do it for you, and since i'm feeling nice here's the rest of the program. if you have any questions about what i did and care about learning it, feel free to ask....

    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    unsigned long fibValue (unsigned long);
    
    int main ()
    {
        unsigned long nth = 0;
        cout << "Enter an integer from 1 to 30:  \n";
    
        do {
            cin >> nth;
            if ((nth < 1) || (nth > 30))
                cout << "Sorry. . . Number isn't in the right range."
                         << "  Try again.\n";
        } while ((nth < 1) || (nth > 30));
    
        cout << "The integer is the " << fibValue(nth) <<" number in the series."
                 << endl;
    
        return 0;
    }
    
    unsigned long fibValue (unsigned long n)
    {
        if (n == 0 || n == 1)
            return n;
        else
             return fibValue (n-1) + fibValue (n-2);
    }
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  3. #3
    *ClowPimp*
    Guest
    Straight recursive fib is horribly inefficient, this method will probably impress your teachers

    Code:
    unsigned long fibValue (unsigned long n)
    {
        static fibVals[MAX_FIB_VAL] = {0}
    
        if (n == 0 || n == 1)
            return fibVals[n] = n;
        else if (fibVals[n] == 0)
            return fibVals[n] = fibValue(n-1) + fibVal(n-2);
        // else
        return fibVal[n];
    }
    Since this code doesnt branch uncontrollably, it will work for much larger numbers

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Smile

    I thank you all for the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM