loop help

This is a discussion on loop help within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, im new here and i would really appreciate some help. I missed 1 lecture and i just cant ...

  1. #1
    Tumo316
    Guest

    loop help

    Hey everyone, im new here and i would really appreciate some help. I missed 1 lecture and i just cant seem to get the loop to work.

    Heres the question:

    The sequence of Fibonacci numbers begins with the integers
    1,1,2,3,5,8,13,21....
    where each number after the first two is the sum of the two preceding numbers.
    Write a loop to display the Fibonacci numbers less than 500.


    Im really stuck! Please help.

  2. #2
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    im not that great at programming, but ill try to give you what help i can:
    i know (or at least im pretty sure) that you are going to want to use an array. define an array of 500 ints (we will call it fibonachi in this example [and please excuse my poor spelling]), with the first 2 having a value of 1. then you will have a loop of something to this effect:
    Code:
    for (int i = 3; i < 500; i++)
    {
      fibonachi[i] = fibonachi[i-1] + fibonachi[i-2];
      cout <<fibonachi[i];
    }
    i think that will work, i didnt actually test the code, just wrote it as i made the post, and as i stated before, im not the greatest at programming, but as far as i can tell it works.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  3. #3
    Tumo316
    Guest
    Thanks for the help but I dont think an array is used at all. At the time of doing that, we hadnt even started arrays and I seriously doubt he would have thrown us in at the deep end.

    Thanks anyway mate.

  4. #4
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    Code:
    #include <iostream>
    
    int main()
    {
      int f;
      int f1 = 0;
      int f2 = 1;
      cour <<"The first number is 0.\nThe second number is 1.\n";
      for (int i = 3; i < 500; i++)
      {
        f = f1 + f2;
        cout <<"The " <<i <<" number is " <<f <<".\n";
        f2 = f1;
        f1 = f;
      }
      return 0;
    }
    <edit>it quickly gets too large to be of any use, though</edit>
    Last edited by Geo-Fry; 05-03-2003 at 01:35 PM.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  5. #5
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Howabout this:
    Code:
    int i, z, tmp;
    
    cout << 1 << endl; //EDIT: this will make the whole sequence correct
    for(i = 0, z = 1; i + z <= 500;) { // allows you to test its < 500 and stop
       cout << (i+z) << endl;  // outpuput to screec
       tmp = i;   // remember what this was
       i = z;     // move the numbers "back" one
       z += tmp;  // Algorithmic increment
    }
    [EDIT] no smileys

    [ANOTHER EDIT] I compiled it and it gave me:
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
    233
    377
    as output. I do believe this is correct.
    Last edited by Inquirer; 05-03-2003 at 02:13 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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