Thread: Fibonacci sequence output statement

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    Unhappy Fibonacci sequence output statement

    HI i need help writing my output statement for fibonacci sequence so far i have how to get the sequence and i must output all array elements with 5 elements per line and in a field width of 10....can someone help me?

    here is what i have so far:
    for (int = 0; i < 30; i++)
    {
    cout << setw(10) << fib[i];
    }
    cout << endl;
    Last edited by chocha7; 11-18-2004 at 05:31 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    for (int = 0; i < 30; i++)
    {
       cout << setw(10) << fib[i];
       if (                  )  //Look up the modulus operator
          cout << endl;
    }
    cout << endl;
    Besides using the modulus operator, you could instead keep a count, and when it reaches 5, output a newline.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    That's exactly what i was thinking....but n e suggestions on where to start?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Check swoopy's post again. There are a couple methods you could use to accomplish this: mod (%) and a resetable counter.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    ok here is what i got :
    for (int i = 0; i < 30; i++)
    {
    cout << setw(10) << fib[i];
    if ( i % 5 == 0)
    cout << endl;
    }
    cout << endl;


    i got a lil bug it outputs a 1 and then it starts again with 1 and outputs the correct info

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use code tags...

    Anyway, I don't quite understand the problem. Is the formatting correct?
    The Fibonacci sequence itself starts with a pair of ones: { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    yes it does but the first 1 is on top n then the other 1 starts off the output correctly

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    if((i + 1) % 5 == 0) 
    ... or ...
    if(i % 5 == 4)
    That ought to do it.
    Last edited by Zach L.; 11-18-2004 at 06:57 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    ahh yes how dum of me...0 % 5 is zero lol....yup the first one does it...thanx fo the help

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    Zach L. -
    The Fibonacci sequence itself starts with a pair of ones: { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc }

    umm the Fibonacci sequence starts with a 0.............its quite essential

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, it can be convenient to define the 0th Fibonacci number F[0] = 0, and in fact, if you use the closed form function to generate Fibonacci's, then F(0)=0, and F(1)=1, so the first (1th) term is really 1, although you can put a 0 before it (obviously) without modifying the sequence.

    So, F(n) = [(1+sqrt(5))^n - (1-sqrt(5))^n]/[sqrt(5) * 2^n]
    Plugging in:
    F(0)=0
    F(1)=1
    etc

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

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 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. sequence
    By braddy in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 02:15 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM