Fibonacci sequence in c++

This is a discussion on Fibonacci sequence in c++ within the C++ Programming forums, part of the General Programming Boards category; He gave it a try, yes I do help, I did point out where to do things and told him ...

  1. #46
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    He gave it a try, yes I do help, I did point out where to do things and told him why. He kept going on his own. You on the other hand saw something that makes no difference in any manner other than because it works you started this whole arguement then ignored the origonal poster. I have nothing more to say to you.

  2. #47
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    He kept going on his own.
    Apparently.

    He seemed to post the same thing over and over until someone did it for him, no matter what anyone said.

    And frankly, I am a bastard, so you'll have to deal with that.

  3. #48
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Since someone else has already done it, and it would be unreasonable for OP to turn in a template programming form, I'll go ahead and post this solution.

    This question gave me an idea.
    Code:
    template <unsigned int N>
    struct Fibonacci
    {
        enum{value = Fibonacci<N - 1>::value + Fibonacci<N - 2>::value};
    };
     
    template <>
    struct Fibonacci<1> 
    {
        enum{value = 1};
    };
    
    template <>
    struct Fibonacci<2> 
    {
        enum{value = 1};
    };
    Is there a way to avoid that second specialization, and specify that the specialization for 1 is for 1 and 2?
    Last edited by User Name:; 02-06-2011 at 09:07 PM.

  4. #49
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Not really. You need a base case for the recursion which is going to occur. The specializations for those values 1 and 2 are such cases.

  5. #50
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by ~Kyo~ View Post
    You do not use () around return.
    I could just as easily state that you do not have a space after an open bracket (as this personally annoys me).
    But we would both be in the wrong because the correct way to phrase it would be:

    "You do not need to have a space after an open bracket"
    or
    "You should not have a space after an open bracket"

    Phrasing it as you did is just rubbish in which case anyone else is right to correct you.
    Code:
    return (((((0)))));
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #51
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by User Name: View Post
    Is there a way to avoid that second specialization, and specify that the specialization for 1 is for 1 and 2?
    Nope. Uou could specialise for zero instead of two, but I don't think that gets you anywhere useful:
    Code:
    template <>
    struct Fibonacci<0> 
    {
        enum{value = 0};
    };
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n-th element of the fibonacci sequence
    By me001 in forum C Programming
    Replies: 22
    Last Post: 09-24-2008, 03:27 AM
  2. Fibonacci Sequence
    By Dogmasur in forum C Programming
    Replies: 15
    Last Post: 08-10-2008, 07:55 AM
  3. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  4. Fibonacci sequence output statement
    By chocha7 in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 10:04 PM
  5. Fibonacci sequence
    By girliegti in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2003, 10:40 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21