Thread: Fibonacci Sequence

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You ... could, I suppose. It's a bit tricky, in that you have to know ahead of time (that is, before the user types anything at all) how long the string is going to be, and you don't. Otherwise, you go back to getting memory on-the-fly. (Same thing for trying to store all the answers in an array -- you won't know when you compile the program how big your array is going to need to be.)

    PS: I'm guessing you type your posts in a word processing program before posting? Not a bad idea, but could you maybe turn off auto-hy-phen-ate?

  2. #2
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by tabstop View Post
    You ... could, I suppose. It's a bit tricky, in that you have to know ahead of time (that is, before the user types anything at all) how long the string is going to be, and you don't. Otherwise, you go back to getting memory on-the-fly. (Same thing for trying to store all the answers in an array -- you won't know when you compile the program how big your array is going to need to be.)

    PS: I'm guessing you type your posts in a word processing program before posting? Not a bad idea, but could you maybe turn off auto-hy-phen-ate?
    Sorry, I'll stop hyphenating. I'm not using a word processor. I was just hyphenating within the window given on this forum. I should preview my posts before posting. Thx for the heads up.

    I keep hearing scanf is bad and using getchar () to wait for an enter key press is bad. Is scanf safe to use here, since I am receiving a simple integer and how do I properly code to wait for a press of the enter key?
    Last edited by Dogmasur; 08-07-2008 at 09:16 PM. Reason: typos; told you I wasn't using a word processor

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Dogmasur View Post
    I keep hearing scanf is bad and using getchar () to wait for an enter key press is bad. Is scanf safe to use here, since I am receiving a simple integer and how do I properly code to wait for a press of the enter key?
    scanf is only bad when you use %s without a width specifier as present it "%80s". %i and the others are fine.

    getchar() is not bad, as far as I know, but using getchar and scanf together can be confusing, since scanf will usually leaves trailing whitespace (and anything else really) in the buffer, and the next getchar call will pick up what's left of the buffer, before going on to the next line.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Dogmasur View Post
    Sorry, I'll stop hyphenating. I'm not using a word processor. I was just hyphenating within the window given on this forum. I should preview my posts before posting. Thx for the heads up.

    I keep hearing scanf is bad and using getchar () to wait for an enter key press is bad. Is scanf safe to use here, since I am receiving a simple integer and how do I properly code to wait for a press of the enter key?
    I would say that when you are working on your own small projects, like this, that using scanf and other 'unsafe' things are ok. But it is good to be aware of their limitations if you tackle something larger.

    As for the fibonacci sequence, I like the recursive solution because of it's simplicity and elegance. Learning a recursive approach is valuable even if it is not always the most optimal performance wise.

  5. #5
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    I'm going to try to put this together with what knowledge I have and can gather from some "light" reading and then put it out for you all to laugh ( I mean comment) at.



    Thx all.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Talking

    simple, just include math.h , and than:

    result = (1/sqrt(5))*((1+sqrt(5))/2)^n-(1/sqrt(5))*((1-sqrt(5))/2)^n ;

    of course n is the variable...

    i've learned this, this week in college.

  7. #7
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by PkD View Post
    simple, just include math.h , and than:

    result = (1/sqrt(5))*((1+sqrt(5))/2)^n-(1/sqrt(5))*((1-sqrt(5))/2)^n ;

    of course n is the variable...

    i've learned this, this week in college.
    I was going to go with a much simpler a+b=c, where I would simply redefine the variables with each pass through my loop.

    What you're getting into is Binet's formula, which might be a bit excessive for what I need. I have some understanding of mathematics and Fibonacci recursion's similarity to the generating polynomial of the recursion. I was asking more about how to code my program than how to compute the variables involved. Sorry if I wasn't clear and thanks for your input.

  8. #8
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by PkD View Post
    simple, just include math.h , and than:

    result = (1/sqrt(5))*((1+sqrt(5))/2)^n-(1/sqrt(5))*((1-sqrt(5))/2)^n ;

    of course n is the variable...

    i've learned this, this week in college.
    I reread through the posts and thought my reply to you sounded snooty. I just wanted to apologize for that and let you know it wasn't meant to sound rude.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Thumbs up

    It 's ok.

    I've just posted the Binet's formula because it's a curiosity, of course it slow down the PC , but if the user whats a number very big,maybe is it better use the formula? or 0+1+1+2+3+5+8+13+21+... ? .

    thats it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fibonacci sequence
    By cph in forum C Programming
    Replies: 57
    Last Post: 04-30-2009, 07:17 AM
  2. Fibonacci sequence
    By MuffanMan123 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2008, 09:15 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. Fibonacci sequence
    By girliegti in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2003, 10:40 PM