Thread: fibonnaci question...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    But does it produce the right answer?

    If the compiler has performed "tail recursion optimization" on the code, then you won't see nice recursive calls, only weird jumps and other effects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Works perfectly for me, maybe you're doing something weird instead of printing the function's output.

    Code:
    #include <iostream>
    
    int recurvsiveFibonacci(int n)
    {
        //there is a breakpoint on each of the lines of code
        if (n == 1 || n == 0)
        {
            return n;
        }
        else
        {
            return (recurvsiveFibonacci(n-1) + recurvsiveFibonacci(n-2));
        }
    }
    
    int main()
    {
        for (int i = 0; i != 10; ++i)
            std::cout << recurvsiveFibonacci(i) << ' ';
    
        std::cout << std::endl;
    
        return 0;
    }
    Output:
    Code:
    0 1 1 2 3 5 8 13 21 34

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh well i was under the impression since it jumped around all the time it was messing up. is there a reason it does this? how does that optimize it?

    lol i guess i just didnt know those were the right answers. i guess i assumed it was wrong because n was jumping all over the place. oh well would of been nice if the teacher explained hey your compiler might jump all over the place doing recursion functions.
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM
  5. A question of color...(not a racial question)
    By Sebastiani in forum Windows Programming
    Replies: 7
    Last Post: 01-15-2003, 08:05 PM