fork download
  1. //Zachary Abdollahi CS1A Chapter 6, P. 373, #15
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE POPULATION GROWTH
  6.  *
  7.  * This program calculates and displays the projected size of a population over
  8.  * a specified number of years based on annual birth and death rates.
  9.  *
  10.  * Computation is based on the formula:
  11.  * N = P + (B * P) - (D * P)
  12.  *
  13.  * INPUT
  14.  * startingPop : Starting size of the population
  15.  * birthRate : Annual birth rate (as a percentage)
  16.  * deathRate : Annual death rate (as a percentage)
  17.  * years : Number of years to project
  18.  *
  19.  * OUTPUT
  20.  * newPop : Calculated population size for each year
  21.  *
  22.  ******************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. // Function Prototypes
  29. int calculatePopulation(int pop, double birthRate, double deathRate);
  30.  
  31. int main()
  32. {
  33. int population; // Starting/current population size
  34. double birthRate; // Annual birth rate
  35. double deathRate; // Annual death rate
  36. int years; // Number of years to display
  37.  
  38. // Get the starting population (must be at least 2)
  39. cout << "Enter the starting population size: ";
  40. cin >> population;
  41. while (population < 2)
  42. {
  43. cout << "Population must be at least 2. Enter starting population: ";
  44. cin >> population;
  45. }
  46.  
  47. // Get annual birth rate (cannot be negative)
  48. cout << "Enter the annual birth rate (e.g., 0.05 for 5%): ";
  49. cin >> birthRate;
  50. while (birthRate < 0)
  51. {
  52. cout << "Birth rate cannot be negative. Enter birth rate: ";
  53. cin >> birthRate;
  54. }
  55.  
  56. // Get annual death rate (cannot be negative)
  57. cout << "Enter annual death rate (e.g., 0.02 for 2%): ";
  58. cin >> deathRate;
  59. while (deathRate < 0)
  60. {
  61. cout << "Death rate cannot be negative. Enter death rate: ";
  62. cin >> deathRate;
  63. }
  64.  
  65. // Get number of years (must be at least 1)
  66. cout << "Enter the number of years to project: ";
  67. cin >> years;
  68. while (years < 1)
  69. {
  70. cout << "Years must be at least 1. Enter number of years: ";
  71. cin >> years;
  72. }
  73.  
  74. // Display table header
  75. cout << "\n----------------------------------------" << endl;
  76. cout << "Year\t\tProjected Population" << endl;
  77. cout << "----------------------------------------" << endl;
  78.  
  79. // Calculate and display populatiob for each year
  80. for (int year = 1; year <= years; year++)
  81. {
  82. population = calculatePopulation(population, birthRate, deathRate);
  83. cout << year << "\t\t" << population << endl;
  84. }
  85.  
  86. return 0;
  87. }
  88.  
  89. /******************************************************************************
  90.   * calculatePopulation *
  91.   * Accepts the previous population, birt rate, and death rate as arguments, *
  92.   * calculates the new population size using N = P + BP -DP, and returns the *
  93.   * result as an integer. *
  94.   *****************************************************************************/
  95. int calculatePopulation(int pop, double birthRate, double deathRate)
  96. {
  97. return pop + (birthRate * pop) - (deathRate * pop);
  98. }
Success #stdin #stdout 0s 5328KB
stdin
100000
0.05
0.02
5
stdout
Enter the starting population size: Enter the annual birth rate (e.g., 0.05 for 5%): Enter annual death rate (e.g., 0.02 for 2%): Enter the number of years to project: 
----------------------------------------
Year		Projected Population
----------------------------------------
1		103000
2		106090
3		109272
4		112550
5		115926