1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Noob programmer help

Discussion in 'Programming & Webmastering' started by Lampmaster, Nov 23, 2009.

  1. Lampmaster

    Lampmaster New Member

    Joined:
    Aug 2, 2009
    Messages:
    156 (0.08/day)
    Thanks Received:
    16
    Location:
    Washington The State
    Hi, I was just wondering if it was possible to take this C++ code and make it to where it sums using the Fibonacci sequence.

     
    Last edited: Nov 23, 2009
  2. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    Need more information. Is the inputted variable "n" the number of Fibonacci series numbers that you want, or do you want to generate numbers in the series until you hit "n"?

    For instance, if n=3 do you want "0,1,1" (first three numbers in the series) or do you want "0,1,1,2,3" (numbers up to 3 in the series)?
     
  3. qamulek

    qamulek New Member

    Joined:
    Feb 7, 2008
    Messages:
    186 (0.08/day)
    Thanks Received:
    26
    Just a quick guess(based on the limited information as pointed out by kreij):
     
    Lampmaster says thanks.
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    Depending upon your needs, you could also do it in an array as they lend themselves nicely to numeric series...
    Code:
    cin >> count;
    
    // Create dynamically sized array based on input.
    int* fibarray;
    fibarray = new int[count];
    
    // Set first two values
    fibarray[0] = 0;
    fibarray[1] = 1;
    
    int i = 2; // Loop variable which starts with 3rd array member
    
    while (i < count)
    {
        fibarray[i] = fibarray[i - 1] + fibarray[i - 2];
        i++;
    }
    
    The above code does not do any error checking in the event that the count variable is set to a value less than 2.
     
  5. Lampmaster

    Lampmaster New Member

    Joined:
    Aug 2, 2009
    Messages:
    156 (0.08/day)
    Thanks Received:
    16
    Location:
    Washington The State
    Thanks for the replies. What Ijust noticed is I don't need to sum the Fibonacci sequence, I just need to output them. So if n = 3 it would output 1,1,2.
     
  6. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    Then either of the above two suggestions will work for you as they both use the inputted number as the number of series results you need.
     
    Lampmaster says thanks.
  7. Lampmaster

    Lampmaster New Member

    Joined:
    Aug 2, 2009
    Messages:
    156 (0.08/day)
    Thanks Received:
    16
    Location:
    Washington The State
    Alright cool, thanks.
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page