Like Tree3Likes

Fibonacci Sequence

This is a discussion on Fibonacci Sequence within the C++ Programming forums, part of the General Programming Boards category; It compiles properly but when I run the program it is a blank screen. Is it because i do not ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    Fibonacci Sequence

    It compiles properly but when I run the program it is a blank screen. Is it because i do not call int fib( int n )?
    "Edit"
    I realize it's timing out now, I just do not understand why.
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int fib ( int n );
    
    
    int main()
    {
        int total = 0;
        int fibn = 0;
    
    
        for ( int i = 0; i < 4000000; i++ )
        {
        fibn = fib(i);
            if (fibn % 3 == 0 || fibn % 5 == 0)
            {
               total += fibn;
            }
    
    
        }
    
    
        cout << "Your fibonacci number will be: ";
        cout << total << "." << endl;
        cout << "Thank you for using the program." << endl;
    
    
    }
    
    
    int fib ( int n )
    {
        if ( n == 0 || n == 1 )
        return n;
    
    
        int fib1 = 0;
        int fib2 = 1;
        int fib = 0;
    
    
        for ( int i = 2; i < n; i++ )
        {
            fib = fib1 + fib2;
            fib1 = fib2;
            fib2 = fib;
        }
    
    
        return fib;
    }
    Last edited by iGuardian; 10-20-2011 at 10:30 PM.

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Have you learned recursion? It would make this program a lot easier and cleaner.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Not yet, we are getting to it in 2 chapters

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by rmatze View Post
    Have you learned recursion? It would make this program a lot easier and cleaner.
    ...but also (AFAIK) .. a lot less efficient.
    Salem and iMalc like this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    I realize it has to do with
    for ( int i = 0; i < 4000000; i++ )
    but my question is how would I go about setting it to add all numbers that can be modded by 3 and or 5 that are less then 4 million.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    You are making this more difficult then it has to be.

    You are going to want to start with fib1 = 0 and fib2 = 1. Then create a loop. Call the fib function which all you want it to do is add two numbers and return that result...that is it. After returning the result put fib2 into fib1 and result into fib2. Print the number and continue with the loop calling the fib function.

  7. #7
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Quote Originally Posted by manasij7479 View Post
    ...but also (AFAIK) .. a lot less efficient.
    True, now that I've looked at it more, a simple do while loop will work just fine.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    so in this case, the function would look like
    Code:
    int fib ( int n ){
        int fib = 0;
        int fib1 = 0;
        int fib2 = 1;
    
    
        fib = fib1 + fib2;
        fib1 = fib2;
        fib2 = fib;
    
    
        return fib;
    }

  9. #9
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Nope, this is all you need:

    Code:
    int fib(int fib1, int fib2)
    {
    return fib1 + fib2;
    }

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    So after looking at some recursion tuts, can this be used for recursion, as dumb of a question as it may be.

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by iGuardian View Post
    can this be used for recursion..
    Well... if you mean .. recursion can be used for this.. the yes!
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    I apologize, I'm only 8 weeks into the course so I may come off more dumb than I really am, C++ isn't coming to me as easy as i thought

  13. #13
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    We all had to learn just like you. I think you should stay away from recursion. Just use a do while loop instead of the for loop.

    Code:
    do 
        {
            //call your fib function and save the return value into say result
            //set fib1 equal to fib2
            //set fib2 equal to result
            
            //print result
            
        }while(result < 4000000);
    manasij7479 likes this.

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by rmatze View Post
    We all had to learn just like you
    So.. you have learnt everything ?.. :P
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    I'm following along the tutorial while i attempt to do it. Is this correct so far provided what you showed me
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    // Recursion will be used for this program.
    int fib ( int fib1, int fib2 )
        {
        return fib1+fib2;
        }
        // The function calls itself until the condition is met.
    
    
    int main()
    {
        int sum_of_all_fibs = 0;
        // The sum will begin at zero, this will be the final output of all numbers
        // found to be modded by 5 or 3 and be less then 4,000,000.
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fibonacci sequence in c++
    By anytime in forum C++ Programming
    Replies: 50
    Last Post: 02-06-2011, 11:40 PM
  2. fibonacci sequence
    By cph in forum C Programming
    Replies: 57
    Last Post: 04-30-2009, 08:17 AM
  3. Fibonacci Sequence
    By Dogmasur in forum C Programming
    Replies: 15
    Last Post: 08-10-2008, 08:55 AM
  4. Fibonacci sequence
    By MuffanMan123 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2008, 09:15 AM
  5. Fibonacci Sequence
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2001, 12:29 PM

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