fork download
  1.  
  2. //Dione Torrevillas CS1A Chapter 6, P. 373, #15
  3. //
  4. /****************************************************************
  5.   *
  6.   * CALCULATE POPULATION GROWTH
  7.   *_______________________________________________________________
  8.   * This program computes the population size over a number of
  9.   * years based on the starting population, annual birth rate,
  10.   * and annual death rate. A function calculates the population
  11.   * for each year.
  12.   *
  13.   * Computation is based on the formula:
  14.   * New Population = Previous Population +
  15.   * (Birth Rate * Previous Population) -
  16.   * (Death Rate * Previous Population)
  17.   *_______________________________________________________________
  18.   * INPUT
  19.   * population : Starting population size
  20.   * birthRate : Annual birth rate percentage
  21.   * deathRate : Annual death rate percentage
  22.   * years : Number of years to display
  23.   *
  24.   * OUTPUT
  25.   * population : Population size for each year
  26.   *
  27.   ****************************************************************/
  28.  
  29. #include <iostream>
  30. #include <iomanip>
  31. using namespace std;
  32.  
  33. // Function prototype
  34. double calculatePopulation(double, double, double);
  35.  
  36. int main()
  37. {
  38. double population; //INPUT - Starting population
  39. double birthRate; //INPUT - Annual birth rate
  40. double deathRate; //INPUT - Annual death rate
  41. int years; //INPUT - Number of years
  42. int year; //OUTPUT - Year being displayed
  43.  
  44. // Get the starting population
  45. cout << "Enter the starting population: ";
  46. cin >> population;
  47.  
  48. // Validate the starting population
  49. while (population < 2)
  50. {
  51. cout << "Enter a population of at least 2: ";
  52. cin >> population;
  53. }
  54.  
  55. // Get the annual birth rate
  56. cout << "Enter the annual birth rate (%): ";
  57. cin >> birthRate;
  58.  
  59. // Validate the birth rate
  60. while (birthRate < 0)
  61. {
  62. cout << "Enter a nonnegative birth rate: ";
  63. cin >> birthRate;
  64. }
  65.  
  66. // Get the annual death rate
  67. cout << "Enter the annual death rate (%): ";
  68. cin >> deathRate;
  69.  
  70. // Validate the death rate
  71. while (deathRate < 0)
  72. {
  73. cout << "Enter a nonnegative death rate: ";
  74. cin >> deathRate;
  75. }
  76.  
  77. // Get the number of years
  78. cout << "Enter the number of years: ";
  79. cin >> years;
  80.  
  81. // Validate the number of years
  82. while (years < 1)
  83. {
  84. cout << "Enter at least 1 year: ";
  85. cin >> years;
  86. }
  87.  
  88. // Convert percentages to decimal values
  89. birthRate = birthRate / 100;
  90. deathRate = deathRate / 100;
  91.  
  92. // Display the table heading
  93. cout << endl;
  94. cout << "Year Population" << endl;
  95. cout << "---------------------" << endl;
  96.  
  97. // Calculate and display population for each year
  98. for (year = 1; year <= years; year++)
  99. {
  100. // Call function to calculate new population
  101. population = calculatePopulation(population,
  102. birthRate,
  103. deathRate);
  104.  
  105. // Display the population for the year
  106. cout << setw(4) << year
  107. << setw(17) << fixed << setprecision(0)
  108. << population << endl;
  109. }
  110.  
  111. return 0;
  112. }
  113.  
  114. // Function definition
  115. // Calculates and returns the new population
  116. double calculatePopulation(double population,
  117. double birthRate,
  118. double deathRate)
  119. {
  120. double newPopulation; // New population size
  121.  
  122. // Calculate the population for one year
  123. newPopulation = population +
  124. (birthRate * population) -
  125. (deathRate * population);
  126.  
  127. return newPopulation;
  128. }
  129.  
Success #stdin #stdout 0.01s 5292KB
stdin
1000
5
2
5
stdout
Enter the starting population: Enter the annual birth rate (%): Enter the annual death rate (%): Enter the number of years: 
Year       Population
---------------------
   1             1030
   2             1061
   3             1093
   4             1126
   5             1159