Thread: n-th element of the fibonacci sequence

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    25

    n-th element of the fibonacci sequence

    n-th element of the fibonacci sequence.
    What are the bugs in below code and how to fix?.

    Code:
    int fib(int n) {
    	int tmp;
    	if (n == 0) return 0;
    	if (n == 1) return 1;
    	tmp = fib(n-2);
    	tmp += fib(n-1);
    	return tmp;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What makes you think there are bugs in the first place?

    That said, what happens if n is negative?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Thanks...Please help me debug this code..

    what compiler i need to use?.

    i put this code in miracle C compiler...gives me bug.
    Last edited by me001; 09-23-2008 at 09:58 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No bug in this code?
    Whether there is a bug or not depends on the specification. As it stands, it is correct, except that negative input would cause an error. Other possible issues include the fact that it is inefficient.

    i put this code in miracle C compiler...gives me bug.
    I have heard that Miracle C is miraculously disastrous, so perhaps it would better if you switched compiler.

    what compiler i need to use?.
    The GNU C compiler is available at zero price (use MinGW port for Windows), as is Microsoft Visual C++ 2008 Express (which comes with a C compiler).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by me001 View Post
    i put this code in miracle C compiler...gives me bug.
    Perhaps Miracle C is the bug? I have no personal experience with this compiler, but there are plenty of free compilers that are BETTER from what I hear. Two of those would be gcc and Microsoft Visual Studio Express Edition.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by me001 View Post
    i put this code in miracle C compiler...gives me bug.
    A compiler doesn't give you a bug, a compiler gives you an error or a warning and if you don't post them, we can't tell you what they mean...!

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Microsoft Visual Studio Express Edition is bulky compiler. i installed...this tool got a learning curve.

    i want a light weight, less resource, free C compiler to learn C...

    i am not going code C...just want to brush up my C skills.
    Last edited by me001; 09-23-2008 at 09:33 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i want a light weight, less resource, free C compiler to learn C...
    The MinGW port of GCC that I listed would be appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    what version i need to download?

    http://gcc.gnu.org/

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For Windows, it would be here: MinGW. Use the installer package.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    downloaded and installed.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    how to start this tool and debug code?

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by me001 View Post
    how to start this tool and debug code?
    RTFM?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    how to start this tool and debug code?
    You run it from the command prompt:
    Code:
    gcc -o program.exe sourcefile.c
    then run program.exe

    But yes, for more information, refer to the GCC website.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    if n is negative code won't work.
    Need to add check to see, n is positive beore passing n to this code.

    what else i need to modify this code to work?
    Last edited by me001; 09-23-2008 at 10:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 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, 12:52 PM
  4. Fibonacci sequence output statement
    By chocha7 in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 11:04 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM