Fibonacci sequence in c++

This is a discussion on Fibonacci sequence in c++ within the C++ Programming forums, part of the General Programming Boards category; Why is your second attempt so much worse than the first one? You were a lot closer before: you only ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Why is your second attempt so much worse than the first one? You were a lot closer before: you only didn't understand how to write a for loop. If you're reading the thread at all, you should have a better idea how now.

  2. #17
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    You NEED to use a loop to get data from the user.

    You NEED to use a loop to display the data.

    You do not use () around return. It is just return 0; 0 is everything went ok any non zero is there was a problem.

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Actually you would NEED use parentheses any time you needed to distinguish between keywords, such as return, and a token, such as 0, without resorting to white space too, ~Kyo~. It's not strange or illegal, even if it isn't going to invoke a function.

  4. #19
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    On return values from main? I mean if there was math where it made sense to use it.

    return 0; is used in every example

    by default

    return 0; means program executed correctly under normal circumstances;

    if you use return 1; or some other value it means it terminated with issues.

    This is normal practice. This is how programming is taught in college and in the AP classes way back in highschool. 0 good non zero lookout. I point out non normal parts those are non normal. Show me a programmer that ALWAYS uses parenthases around ALL returns even for int main().

  5. #20
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    ima need help. this is not coming out right.

    #include <iostream>
    using namespace std;

    int main (int argc, char* argv[])
    {
    int fib1 = 0;
    int fib2 = 1;
    int fib3;
    int numbers;


    cout<<"How many numbers would you like to see";
    cin >> numbers;

    int count;
    for (count = 1; count <= 100; count = count=1)
    cout << count << " ";

    fib3 = fib1 + fib2;
    cout << fib3;
    fib1 = fib2;
    fib2 = fib3;
    system("pause");
    return 0;
    }

  6. #21
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Your loop executes forever...
    you need braces around the code that you want to execute.
    probably down to the fib2 = fib3; line...

    count = count=1
    count is always 1 this loop will never break. You probably ment count = count + 1.

  7. #22
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    how do i make the fibonacci sequence?


    #include <iostream>
    using namespace std;

    int main (int argc, char* argv[])
    {
    int fib1 = 0;
    int fib2 = 1;
    int fib3;
    int numbers;


    cout<<"How many numbers would you like to see";
    cin >> numbers;
    {
    int count;
    for (count = 1; count <= 100; count = count+1)
    cout << count << " ";

    fib3 = fib1 + fib2;
    cout << fib3;
    fib1 = fib2;
    fib2 = fib3;
    }
    system("pause");
    return 0;
    }

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    On return values from main? I mean if there was math where it made sense to use it.

    return 0; is used in every example
    It doesn't matter if you think that is true, it is a fact.

    "return0" would not be correct like "return(0)" or "return 0" is. Thus, parentheses for grouping is not illegal in C++. There is no harm in it, other than it being extraneous under most circumstances.

  9. #24
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    @whiteflags
    It is syntax that would be graded wrong - generally unless teachers are lax now.
    Also I don't think I ever said write return0; with no space. Get a life your arguement is unbased and unneeded.
    @anytime
    You already are move the open breace down to just after the for loop and it will work. You will need to display the first two numbers prior to the for loop though.
    Last edited by ~Kyo~; 02-06-2011 at 08:07 PM.

  10. #25
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    how do i write the code for the fibonacci sequence?

  11. #26
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    This is your code it works even prints the new number out as long as this is in the loop you have it.
    Code:
    fib3 = fib1 + fib2;
    cout << fib3;
    fib1 = fib2;
    fib2 = fib3;

  12. #27
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    How are you posting without code tags, anytime?
    Code:
    #include <iostream>
    using namespace std;
    
    int main (int argc, char* argv[])
    {
        int fib1 = 0;
        int fib2 = 1;
        int fib3;
        int numbers;
    
    
        cout<<"How many numbers would you like to see";
        cin >> numbers;
        int count;
        for (count = 1; count <= 100; count = count+1)
        {
            cout << count << " ";
    
            fib3 = fib1 + fib2;
            cout << fib3 << " ";
            fib1 = fib2;
            fib2 = fib3;
    
        }
        system("pause");
        return 0;
    }
    Why 100 and not number?

  13. #28
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    the sequence is not coming out right

  14. #29
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Makes me wonder why he has 1 and not 2 as a start as well shrug minor things to change for him. He just needs to get everything straight and then change the little bits around.

  15. #30
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Quote Originally Posted by ~Kyo~ View Post
    @whiteflags
    It is syntax that would be graded wrong - generally unless teachers are lax now.
    Then the teachers are wrong, and so are you.

    Does
    Code:
    int main()
    {
       return(0);
    }
    result in any compiler messages or bugs? Have you really not seen a return value wrapped in parens before? There are lots of places where spaces are used, where equally valid though perhaps user unfriendly is parens.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n-th element of the fibonacci sequence
    By me001 in forum C Programming
    Replies: 22
    Last Post: 09-24-2008, 03:27 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, 11:52 AM
  4. Fibonacci sequence output statement
    By chocha7 in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 10:04 PM
  5. Fibonacci sequence
    By girliegti in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2003, 10:40 PM

Tags for this Thread


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