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+2 = Fi + Fi+1 for i = 0, 1, 2, 3, ... In other words, each number is the sum of the previous two integer 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 (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 10 pounds of crud; in 20 days 10 pounds (the sum of the previous two 10 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 9 days and then increases every tenth day.
Hi, I have to write this program using a do-while loop, a for loop, and a while loop. I have it programmed with a do while, but i am struggling on how to do the others without making much more work for myself. Is there an easy way to do this? Can someone show me how? The program below works.
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
|
#include <iostream>
using namespace std;
int main()
{
const int TIME_PERIOD = 5 ; //declaring time period to be 5 days
//intro to program
cout << "Hello, this program will tell you how big your green crud " <<
"population will grow in a certain number of days.\n\n";
//looping program using do_while loop
double initialSize;
char answer; //response for loop
do
{
//asking user for weight of crud in pounds
cout << "Please enter the initial weight (in pounds) of your population of green crud: ";
cin >> initialSize;
cout << "\n";
// asking user to enter the amount of number of days
int num_of_days;
cout << "Now, please enter the number of days your green crud population will grow: ";
cin >> num_of_days;
int count = num_of_days / TIME_PERIOD; // making sure green crud grows every five days.
cout << "\n----------------------------------------------------------------------------------" << endl;
cout << "Result:\n";
double f_1 = 0;
double f_2 = initialSize;
for(int i = 0 ; i < count ; i++)
{
// fibonacci code.
double temp = f_1 + f_2 ;
f_1 = f_2 ;
f_2 = temp ;
}
//outputting
cout << "\nAfter " << num_of_days << " days, your green crud population will have grown to be "
<< f_2 << " lbs.\n\n";
cout << "----------------------------------------------------------------------------------\n";
//asking user to calculate again
cout << "\nWould you like to try a different weight? Please enter Y for Yes or N for No. ";
cin >> answer;
}
while ( answer == 'y' || answer == 'Y' );
cout << "\nProgram Terminated.\n" ;
return 0;
}
|