Fibonacci sequence trouble.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;

int main()
{
    //declaring all variables
    int initial_size; // in lbs.
    int num_of_days;
    int time_period;
    long double f_value;
    long double f_1;
    long double f_2;
    long double count;
    time_period == 5; // labeling time period as five days.
    count == num_of_days / time_period; // making sure crud grows every five days.
    char ans;  // response for loop

    //intro to program
    cout << "Hello, This program will tell you \n" ;
    cout <<"how big your green crud population will \n" ;
    cout <<"grow in a certain number of days. \n" ;
    cout << "\n" ;
    //looping program using do_while loop
    do
    {
        //asking user for weight of crud in pounds
        cout << "Enter the weight of your green crud in pounds: \n" ;
        cin >> initial_size;
        cout << "\n";
        // asking user to enter the amount of number of days
        cout << "Now, enter the number of days your crud population \n" ;
        cout << "will grow: \n" ;
        cin >> num_of_days;
         // code for determining crud size
         if ( count <= 1 )
         {
             cout << "In " << num_of_days ;
             cout << " your green crud is " << f_2 << " lbs. \n";
         }
         else
         {
             // fibonacci code.
             f_value = f_1 + f_2;
             f_1 = f_2;
             f_2 = f_value;
            //outputting
            cout << "In " << num_of_days << " days " << " your green crud is " << f_value  << " lbs.\n";
            cout << "\n" ;
         }
         //asking user to calculate again
         cout <<"Would you like to try a different weight? \n" ;
         cout << "( Y for yes anything else for no ) \n" ;
         cin >> ans;

    }
    while ( ans == 'y' || ans == 'Y' );
    {
        cout << "\n" ;
        cout << "Program Terminated. \n" ;
    }
    return 0;
}


I am supposed to do this problem below.
The Fibonacci numbers F(n) are defined as follows F(0) is 1, F(1) is 1,
and F(i+2) = F(1) + F(i+1)
i = 0,1,2,..... In other words,each number is the sum of the previous two numbers. The first few Fibonacci numbers are 1,1,2,3,5, and 8. One place that these numbers occur is as certain population growth rates. If a population has no deaths, then the series shows the size of the population after each time period. It takes an organism two time periods to mature to reproducing age, and then the organism reproduces once every time period. The formula applies most straightforwardly to asexual reproduction at a rate of one offspring per time period.

Assume that the green crud population grows at this rate and has a time period of 5 days. Hence, if a green crud population starts out as 10 pounds of crud, then in five days there is still 10 pounds of crud; in 10 days there is 20 pounds of crud, in 15 days 30 pounds, in 20 days 50 pounds, and so forth. Write a program that takes both the initial size of a green crud population (in pounds) and a number of days as input, and outputs the number of pounds of green crud after that many days. Assume that the population size is the same for four days and increases every fifth day. Your program should allow the user to repeat this calculation
as often as desired.
Last edited on
It doesn't give me the value I want I don't know why if someone could shed some light that would be great .
Help please :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int initial_size; // in lbs.
int num_of_days;
int time_period;
long double f_value;
long double f_1;
long double f_2;
long double count;
time_period == 5; // labeling time period as five days.

// *** what is the value of num_of_days at this point?
count == num_of_days / time_period;  

// ...

// loop

             // fibonacci code.
             // *** what are the values of f_1 and f_2 the first time around?
             f_value = f_1 + f_2;
             // ... 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
    //declaring all variables
    int initial_size; // in lbs.
    int num_of_days;
    int time_period;
    long double f_value;
    long double f_1;
    long double f_2;
    long double count;
    time_period == 5; // labeling time period as five days.
    count == num_of_days / time_period; // making sure crud grows every five days.
    f_1 = 0;
    f_2 = 0;
    f_value = 0;

    char ans;  // response for loop 

Well the number of days is entered by the user and divided by five because the green crud grows according to the Fibonacci sequence every 5 days. And I set the values to "0" will that help?
someone told me I need a for function? but i'm not sure how to work that in.
> Well the number of days is entered by the user ...
> And I set the values to "0" will that help?

No. The number of days is entered by the user and you have to use that value.

1
2
cin >> num_of_days ; // first
count == num_of_days / time_period ;  // and then 


Likewise:
1
2
cin >> initial_size ; // first
f_1 = f_2 = initial_size  ; // and then 


Prefer something like const int time_period = 5 ;
over
1
2
3
int time_period ;
// ...
time_period == 5; // labeling time period as five days. 


In general, do not have all the variables, most of the uninitialized, right at the top. Postpone defining a variable till you reach the point where you know how to initialize it.
I tried to use const int but it says that const is uninitialized..
thank you though .. i appreciate your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;

int main()
{
    //declaring all variables
    int initial_size; // in lbs.
    int num_of_days;
    const int time_period = 5;//declaring time period to be 5 days
    long double f_1;
    long double f_2;
    long double count;
    char ans;  // response for loop

    //intro to program
    cout << "Hello, This program will tell you \n" ;
    cout <<"how big your green crud population will \n" ;
    cout <<"grow in a certain number of days. \n" ;
    cout << "\n" ;
    //looping program using do_while loop
    do
    {
        //asking user for weight of crud in pounds
        cout << "Enter the weight of your green crud in pounds: \n" ;
        cin >> initial_size;
        cout << "\n";
        // asking user to enter the amount of number of days
        cout << "Now, enter the number of days your crud population \n" ;
        cout << "will grow: \n" ;
        cin >> num_of_days;
        count == num_of_days / time_period; // making sure crud grows every five days.

         // code for determining crud size
         if ( count <= 1 )
         {
             cout << "In " << num_of_days ;
             cout << " your green crud is " << f_2 << " lbs. \n";
         }
         for ( initial_size = 1; initial_size <= count; initial_size++ )
         {
             // fibonacci code.
             initial_size = f_1 + f_2;
             f_1 = f_2;
             f_2 = initial_size;
            //outputting
            cout << "In " << num_of_days << " days " << " your green crud is " << initial_size  << " lbs.\n";
            cout << "\n" ;
         }
         //asking user to calculate again
         cout <<"Would you like to try a different weight? \n" ;
         cout << "( Y for yes anything else for no ) \n" ;
         cin >> ans;

    }
    while ( ans == 'y' || ans == 'Y' );
    {
        cout << "\n" ;
        cout << "Program Terminated. \n" ;
    }
    return 0;
}

I feel really incapable right now. it now doesnt output the number i want at all in fact it skips the whole cout statement
Look Im just lacking effort right now .. i dont expect you to do this for me .. all i have to do is go back to the book n read..
> all i have to do is go back to the book n read..

Yes. Make sure you do that. And then you may want take to have a quick glance at:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

int main()
{
    const int time_period = 5 ; //declaring time period to be 5 days

    //intro to program
    std::cout << "Hello, This program will tell you\nhow big your green crud "
            "population will\ngrow in a certain number of days.\n\n" ;

    //looping program using do_while loop
    char ans ;  // response for loop
    do
    {
        //asking user for weight of crud in pounds
        std::cout << "Enter the weight of your green crud in pounds: " ;
        double initial_size ; // in lbs.
        std::cin >> initial_size ;
        std::cout << "\n";

        // asking user to enter the amount of number of days
        std::cout << "Now, enter the number of days your crud population will grow: " ;
        int num_of_days ;
        std::cin >> num_of_days;
        int count = num_of_days / time_period; // making sure crud grows every five days.

        double f_1 = initial_size ;
        double f_2 = initial_size ;

        for( int i = 0 ; i < count ; ++i )
        {
            // fibonacci code.
            double temp = f_1 + f_2 ;
            f_1 = f_2 ;
            f_2 = temp ;
        }

        //outputting
        std::cout << "\nIn " << num_of_days << " days your green crud will be "
                  << f_2  << " lbs.\n\n" ;

        //asking user to calculate again
        std::cout << "Would you like to try a different weight?\n"
                     "( enter one character: Y for yes, anything else for no)\n" ;
        std::cin >> ans ;
    }
    while ( ans == 'y' || ans == 'Y' ) ;

    std::cout << "\nProgram Terminated.\n" ;
}


Here

count == num_of_days / time_period; // making sure crud grows every five days.


there is a comparision not the assignment
Well I read! ha ! took me like three days because I kept putting it off.. and wow thank you.. Your reference helped me out a lot... I realized I make my programs way more complicated than they need to be. Not sure why .. Most likely its my logic and habit of over analyzing. I am really great-full for you knowledge and help. Much appreciated you gave me way more than I expected and I appreciate your time.
Last edited on
Topic archived. No new replies allowed.