fork download
  1. //Eric Bernal CS1A Chapter 6, P. 373, #15
  2. //
  3. /*******************************************************************************
  4.  * CALCULATE POPULATION SHIFT
  5.  * _____________________________________________________________________________
  6.  * This program calculates the shift in a population based off of a given number
  7.  * of years, annual birth rate, and the annual death rate.
  8.  * _____________________________________________________________________________
  9.  * Input
  10.  * popBefore : The population before the shift.
  11.  * birthRate : The annual birth rate as a percentage.
  12.  * deathRate : The annual death rate as a percentage.
  13.  * years : The number of years that has passed.
  14.  *
  15.  * Output
  16.  * popAfter : The population after the shift.
  17.  ******************************************************************************/
  18.  
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. // Function Prototype
  23. float popShift(int before, float birthRate, float deathRate);
  24.  
  25. int main() {
  26. // Data Dictionary - Initialize all Variables
  27. float popBefore; //INPUT : The population total before
  28. float birthRate; //INPUT : The annual birth rate as a percentage.
  29. float deathRate; //INPUT : The annual death rate as a percentage.
  30. float years; //INPUT : The number of years of change
  31. float popAfter; //OUTPUT: The population total after.
  32.  
  33. // Obtain all values prompt
  34. cout << "Please enter the following: \nPopulation: \nBirth Rate: \nDeath Rate: "
  35. << "\n Years: \nPlease enter the values a positive whole numbers." << endl;
  36. cin >> popBefore >> birthRate >> deathRate >> years;
  37.  
  38. //INPUT VALIDATION
  39. if (popBefore < 1)
  40. {
  41. cout << "Please enter a population greater than 0: " << endl;
  42. cin >> popBefore;
  43. }
  44.  
  45. if (birthRate <= 0)
  46. {
  47. cout << "Please enter a whole number greater than 0." << endl;
  48. cin >> birthRate;
  49. }
  50.  
  51. if (deathRate <= 0)
  52. {
  53. cout << "Please enter a whole number greater than 0." << endl;
  54. cin >> deathRate;
  55. }
  56.  
  57. if (years < 1)
  58. {
  59. cout << "Please enter a whole number greater than 0." << endl;
  60. cin >> years;
  61. }
  62.  
  63. // Create Population Table Chart
  64. popAfter = popBefore;
  65. cout << "Year 0: " << popAfter << endl;
  66.  
  67. // For loop to count the years
  68. for (int i = 1; i <= years; i++)
  69. {
  70. popAfter = popShift(popAfter, birthRate, deathRate); //Function Call
  71. cout << "Year " << i << ": " << popAfter << endl;
  72. }
  73. return 0;
  74. }
  75.  
  76. /******************************************************************************
  77.  * The purpose of the following function is to calculate the population shift.
  78.  ******************************************************************************/
  79. float popShift(int before, float birthRate, float deathRate)
  80. {
  81. return before * (1 + (birthRate/100) - (deathRate/100));
  82. }
Success #stdin #stdout 0.01s 5280KB
stdin
1000
5
10
10
stdout
Please enter the following: 
Population: 
Birth Rate: 
Death Rate: 
 Years: 
Please enter the values a positive whole numbers.
Year 0: 1000
Year 1: 950
Year 2: 901.55
Year 3: 855.95
Year 4: 812.25
Year 5: 771.4
Year 6: 732.45
Year 7: 695.4
Year 8: 660.25
Year 9: 627
Year 10: 594.7