Question: Did I write this program correctly and is it computing the correct amount of green crud (the input is included in the description)? /* Program
Did I write this program correctly and is it computing the correct amount of green crud (the input is included in the description)?
/* Program : Fibonacci Green Crud Population Program Using For Loop Description : 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, while, and do-while loop). The Fibonacci numbers Fn are defined as follows: F_0 is 1, F_1 is 1, and F_i = F_i-1 + F_i-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 at 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 10 days. Hence, if a green crud population starts out as 5 pounds of crud, then in 10 days there is still 5 pounds of crud; at the end of the next 10 days, there is still 10 pounds of crud; in 10 more days, we have 20 pounds (the sum of the previous two 10 day periods). The next 10 days will produce 30 pounds.
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 9 days and then increases every tenth day.
Input to the crud program: start with 5 pounds of green crud and display how much crude we have after 200 days. Run each of the three versions of the program with 200 days. You CANNOT use arrays for this program. */
#include
using namespace std;
int initialGreenCrud; int daysToSimulate;
void getData() { cout << "Please enter the initial amount of green crud (in pounds): "; cin >> initialGreenCrud;
cout << "Please enter number of days (an integer value) to simulate: "; cin >> daysToSimulate; }
int computeGreenCrud(int initialGreenCrud, int day) { int fibonacciNum1 = 0; int fibonacciNum2 = initialGreenCrud; int fibonacciNum3 = 0;
for (int i = 1; i <= daysToSimulate; ++i) { if (i % 10 == 0 && i != 0) { fibonacciNum3 = fibonacciNum1 + fibonacciNum2; fibonacciNum1 = fibonacciNum2; fibonacciNum2 = fibonacciNum3; }
day = day + 1; }
return fibonacciNum3; }
void printData() { ofstream myfile; myfile.open("Fibonacci GreenCrud Population Using For Loop Results.txt"); int fibonacciNum3 = computeGreenCrud(initialGreenCrud, daysToSimulate); int day = daysToSimulate; myfile << "After day " << day << ", your green crud population will have grown " << fibonacciNum3 << " pounds." << endl; }
int main() { getData(); printData();
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
