Thread: Fibonacci -- suggestions please

  1. #1
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92

    Fibonacci -- suggestions please

    Hello all. I am still pretty new to C++ and I made this Fibonacci sequence thing.

    What would you recommend to prevent a segmentation fault if the first to vector elements are not populated, or rather, how would you ensure the first two elements were populated?

    Code:
    #include <iostream>
    #include <vector>
    #include <iomanip>
    
    using namespace std;
    
    int main(){
    	vector<double> fib;
    	double stop = 200;
    	double i = 1;
    
    	fib.push_back(1);
    	fib.push_back(1);
    
    	while(i++ <= stop)
    		fib.push_back(fib[i-1] + fib[i-2]);
    
    	for(i = 0; i < fib.size(); i++)
    		cout << setprecision(50) << fib[i] << endl;
    
    	cout << endl;
    
    	return 0;
    }
    TIA!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know what your question is. If the first two elements aren't populated, you can't do the Fibonacci sequence, so you shouldn't ever get into the situation.

    But I suppose you can check that fib.size is at least 2 before trying to do the loop.

  3. #3
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    I guess what I am really asking is: is that how you would implement it , if there was some way I was missing that the first two values could be filled or a better way the program could be written.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Kudose View Post
    I guess what I am really asking is: is that how you would implement it , if there was some way I was missing that the first two values could be filled or a better way the program could be written.
    but you have it

    Code:
    fib.push_back(1);
    fib.push_back(1);
    What you have wrong - is

    double i = 1;

    1. Why index is double?
    2. Why it is 1 if you want to access element i-2?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    1. It should be an int, it was left over from messing around.

    2. I see the logic error in that. i should be 2.

    Thanks.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Kudose View Post
    2. I see the logic error in that. i should be 2.
    No, you had it right as it was. It is initialised to 1 and then incremented to two in the while loop condition.
    The only bug is using double instead of int.
    An explicit push_back of the two 1's before the loop gets underway is the sensible way to do it, if you need the values all in a vector. In your code example you can just output them as you go and forget the vector though.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fibonacci prime
    By dbzx in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 11:13 AM
  2. fibonacci using recursion?
    By n3cr0_l0rd in forum C Programming
    Replies: 12
    Last Post: 02-25-2009, 08:49 AM
  3. Recursive Fibonacci, some help needed
    By cwafavre in forum C Programming
    Replies: 8
    Last Post: 11-04-2007, 02:20 PM
  4. fibonacci problem using while loop
    By galmca in forum C Programming
    Replies: 6
    Last Post: 10-02-2004, 05:12 AM
  5. void fibonacci() - Is This Possible?
    By Not Yet in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 10:24 PM