Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By laserlight
  • 1 Post By anon

Fibonacci

This is a discussion on Fibonacci within the C++ Programming forums, part of the General Programming Boards category; Hi Guys I am fairly new to C++ and although I have been doing pretty well, I am now stuck ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Fibonacci

    Hi Guys I am fairly new to C++ and although I have been doing pretty well, I am now stuck on a certain code.

    The problem given to us is stating :

    Write a C++ Program that outputs the Fibonacci numbers that are less than a user specified value. Each Fibonacci number is the sum of its two predecessors. The first two Fibonacci numbers are are 1 and 1. Thus, the sequence begins with;
    1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.
    The program should keep asking the user for an upper limit and display the Fibonacci numbers that re less than that upper limit until the user enters an upper limit of 1 or less to quit
    Output each Fibonacci number on a separate line.


    I have been working on this for a while now and so far, this is what I have come up with. I know its not much, but I am trying my hardest to work on it.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    
    using namespace std;
    
    
    int main()
    {
    	int fib;
    	int fib1 = 0;
    	int fib2 = 1;
    	int fib3;
    	int number;

    Now i understand what the problem is asking for, I just don't know how to program it...This is due tomm, in around 23 hours and I would like to turn it in

    Thank You!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Keep in mind that this forum (and, thankfully, every other programming forum I've ever seen) has (i) strict rules against doing homework assignments for people and (ii) users who basically always follow that rule, so we can only give you "general guidance", although if you have specific problems (e.g. I don't know why this line segfaults/this function doesn't return the value I expect) we'll be happy to help.

    Anyway, what's your specific problem - what exactly are you having problems with? Getting user input? How to compute a Fibonacci number? How to structure your program?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Oh I'm sorry, I wasn't asking anyone to do my homework...I was just hoping that someone would be able to guide me in the right direction...if someone else does my homework, then I don't really learn and I want to learn. My first question is how would I set a limit so that say, if I want a limit of 30, my output should be 1 1 2 3 5 8 13 21. I'm not exactly sure how to set it as.

    Code:
    int fib, fib1, fib2, fib3, fibn;
    
    cout << "Please enter an upper limit (1 or less to quit) \n" << endl;
    cin >> fibn;

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Fib

    Oh I'm sorry, I wasn't asking anyone to do my homework...I was just hoping that someone would be able to guide me in the right direction...if someone else does my homework, then I don't really learn and I want to learn. My first question is how would I set a limit so that say, if I want a limit of 30, my output should be 1 1 2 3 5 8 13 21. I'm not exactly sure how to set it as.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    I'm thinking using an array would be the easiest way for accessing the numbers so often in the same group. As for the max number I'm thinking a Do..While loop would work. I'm just a novice when it comes to C++ but from what I gathered that should work. I'm actually going to try and make this now just to see if I can. Thanks for the challenge.
    Last edited by Blade3575; 11-29-2011 at 06:57 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    You don't need an array since you can just output the sequence from scratch each time on demand, but it does make sense to use an array to cache the part of the sequence that was previously generated.
    Blade3575 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Fib

    We actually haven't even learnt arrays yet "/ We JUST Started learning loops and thats it. I'm completely lost in loops, so I'm trying to make this program blind.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote Originally Posted by Pookie
    We actually haven't even learnt arrays yet
    Okay, then the "output the sequence from scratch each time on demand" approach is the way to go for you.

    Quote Originally Posted by Pookie
    We JUST Started learning loops and thats it. I'm completely lost in loops
    Start by writing a program to compute and print say, the first 10 Fibonacci numbers.
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A start might also be to write, say, a program that outputs all odd numbers up to users input.
    iMalc likes this.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Fibonacci
    By Thedog in forum C Programming
    Replies: 3
    Last Post: 01-17-2011, 05:04 PM
  2. Fibonacci
    By Paged in forum C Programming
    Replies: 2
    Last Post: 12-22-2010, 03:42 PM
  3. nth fibonacci
    By meriororen in forum C Programming
    Replies: 1
    Last Post: 08-10-2009, 05:13 AM
  4. sum of fibonacci
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 09:28 AM
  5. fibonacci(10,000)
    By blindman858 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2005, 12:03 AM

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