C++ Fibonacci's Rabbits!!!!!

This is a discussion on C++ Fibonacci's Rabbits!!!!! within the C++ Programming forums, part of the General Programming Boards category; Hey, i wrote this program for fibonacci's sequence, it works, but im having a problem completing a modification of it, ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    C++ Fibonacci's Rabbits!!!!!

    Hey, i wrote this program for fibonacci's sequence, it works, but im having a problem completing a modification of it, i want to write 2 programs , i have done the first , i need help on the second.

    First problem: Write a program that accepts user input for the number of months, and computes the number of pairs of rabbits at that month. Use a for loop and within the for loop output the running total. (( ACCOMPLISHED ))

    Second Problem: First problem: Write a program that accepts user input for the number of pairs of rabbits, and computes the minimum number of months it woud take to have that many pairs. Use a for loop and within the for loop output the running total.
    (( NEED HELP ))

    The C++ code is here:

    Code:
    /*****************************************************************************************
    **************************************ASSIGNMENT 2****************************************
    ******************************************************************************************
    *                                                                                        *
    *      Student Name:       Colin Raphael                                                 *
    *      Student Number:     100727139                                                     *
    *      Description:        This program uses fibonacci's sequence to determine the number*
    *                          of pairs of rabbits by a given input of month.                *
    *                                                                                        *
    ******************************************************************************************
    ******************************************************************************************
    *****************************************************************************************/
    
    #include <iostream> 
    
    using namespace std;
     
    void main(void)
    {
    int f1=1,f0=0,f,x,i;                     //Declaring used variables
    cout << "Enter the number of months: ";  //Prompts user for number of months
    cin >> x;                                //Stores variable
    
    for (i=1; i<=x; i++)                     //loop statement
    {
    f = f1 + f0;
    cout <<"In month " << i << " there are " << f << " pair(s) of rabbits." << endl; // Outputting loop statment
     break;                                                                          //stop loop
    }
    
    for (i=2; i<= x; i++)
    {
    f = f1 + f0;
    f0 = f1;
    f1 = f;
    cout <<"In month " << i << " there are " << f << " pair(s) of rabbits." << endl;      // Outputting loop statement
    }
    
    cout <<"After " << x << " month(s) there are " << f << " pair(s) of rabbits."<<endl;  // conclusion
    }
    >> I need somebody to modify it to satisfy problem 2 , please and thanks for your help
    Last edited by colinraff; 10-25-2006 at 11:18 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    ok never mind i figured it out, this is what motivates me to keep programming , learning new stuff on the way!

    thanks anyway if any of you were going to help!

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Just one minot point, if you indent your code it does make it much easier to maintain, read and most importantly debug

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Oh and

    Code:
    void main ( void )
    should be

    Code:
    int main ( void )
    As main must return an int value

Popular pages Recent additions subscribe to a feed

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