Crud Program using Fibonacci numbers

Hello, we are supposed to write a program to find the population of crud after a certain period of time using 3 different types of loops. I will post the question:
The following program is to be written with a loop. You are to write this program three times and each time with a different loop control structure (for loop, while and do-while).
The Fibonacci numbers Fn are define as follows. F0 is 1, F1 is 1, and Fi = Fi-1 + Fi-2 for i = 0, 1, 2, 3, ... In other words, each number is the sum of the previous two integer numbers for F2. 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 (thus the reason for two 1's before you start adding). The formula applies most straightforwardly to a sexual 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 3 days. Hence, if a green crud population starts out as 3 pounds of crud, then in 5 days there is still 3 pounds of crud; at the end of the second 5 days there is still 3 pounds of crud; (ie the first two Fib numbers are 3) then in 5 more days we have 6 pounds (the sum of the previous two 5 day periods). Write a program that takes both the initial size of green crud population (in pounds) and a number of days as input, and output the number of pounds of green crud after that many days of growth. Assume that the population size is the same for 5 days and then increases every fifth day.

Input to the crud program: start with 3 pounds of green crude and display the how much crude we have after 190 days. Run each of the three versions of the program with 190 days. You can NOT use arrays for this program.
Now, what happens when you run the program for 250 days when you use the simple data type ‘int’? (Not long int or double data types please.)


I have no idea how to implement that into a loop for the formula. I wrote just the bare bones for now, but if I could see how to start it, that would really help me get going on this. Thanks everyone. We can't use arrays and we haven't learned vectors and all that. I could do functions though.

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
 #include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int days,i;
    int crud, total;
    
    cout<<"How many days are we testing for?: "<<endl;
    cin>>days;
    
    cout<<"How many pounds of crud to start?: "<<endl;
    cin>>crud;
    
    for(i=0;i<days;i++)
    {
       //have no idea what to put here 
    }
    cout<<total;
    
    //table 
    cout<<endl;
    cout<<"-----------------------------------------------------------------"<<endl;
    cout<<"Initial Amount  "<<setw(16)<<"Number of Days "<<setw(20)<<"Total Crud After"<<endl;
    cout<<"-----------------------------------------------------------------"<<endl;
    cout<<setw(5)<<crud<<"lbs"<<setw(18)<<days<<endl;
    return 0;
}
1
2
3
4
for ( int day = WHICH_DAY_DO_YOU_START_ON? ; day < WHICH_DAY_DO_YOU_STOP_ON ; day++)
{
  amount = amount + HOW_MUCH_DO_YOU_ADD_TODAY_ON_DAY_"day" ?
}


 
HOW_MUCH_DO_YOU_ADD_TODAY_ON_DAY_"day"
is what you need to work out. I'll give you a hint; you're going to need some more variables.
I feel like I'm very close..the numbers are just off and I've tried re-arranging the order, changing the day to start at 1..etc..

1
2
3
4
5
6
7
8
for (day=0; totalDays>day; day+=5)
{
     total=first+second;
     first=second;
     second=total;
    
}


It works for the first few then it is off by 3 and then increases to be off.
To compute the Fibonacci numbers with a loop, you have to remember the previous two values and update them in the loop. Here is code that does that. You'll have to modify this to account for the fact that a time period happens every 5 days, and the fact that the amount of crud starts at 5 instead of 1.

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

using namespace std;
int
main()
{
    int n=1;
    cout << "Enter n: ";
    cin >> n;

    // Compute Fibonacci number F(n)
    int f1=1;			// f(n-1)
    int f2=1;			// f(n-2)
    for (int i=2; i<=n; ++i) {
	int fi = f1 + f2;	// compute new value
	cout << "F" << i << " = " << fi << '\n'; // and print it out
	f2 = f1;				 // now update f2 and f1
	f1 = fi;
    }
    return 0;
}

Thank you! I took what you posted and modified it in mine and it works! I just needed to change the second=first and then first=total.

I appreciate it!

1
2
3
4
5
6
7
for (day=0; totalDays>day; day+=5)
{
     total=first+second;
     second=first;
     first=total;
    
}
Topic archived. No new replies allowed.