fork download
  1. // Elaine Torrez CS1A Chapter 6, P.374, #15
  2. // *****************************************************************************************
  3. // * POPULATION *
  4. // *--------------------------------------------------------------------------------------- *
  5. // * This program calculates and displays the projected size of a population over a given *
  6. // * number of years, based on the starting size, annual birth rate, and annual death rate. *
  7. // * The formula used to find the new population each year is: *
  8. // * *
  9. // * N = P + (B * P) - (D * P) *
  10. // * *
  11. // * where N = new population, P = current population, *
  12. // * B = birth rate, and D = death rate. *
  13. // *--------------------------------------------------------------------------------------- *
  14. // * INPUT *
  15. // * startPop : starting population size (>= 2) *
  16. // * birthRate: annual birth rate (non-negative, as a decimal) *
  17. // * deathRate: annual death rate (non-negative, as a decimal) *
  18. // * years : number of years to project (>= 1) *
  19. // *--------------------------------------------------------------------------------------- *
  20. // * OUTPUT *
  21. // * Displays the population size for each projected year. *
  22. // *****************************************************************************************
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. #include <limits> // for input validation
  27. using namespace std;
  28.  
  29. // ---------------- Function Prototype ----------------
  30. double calcPopulation(double prevPop, double birthRate, double deathRate);
  31.  
  32. // ---------------------- MAIN -------------------------
  33. int main()
  34. {
  35. double startPop, birthRate, deathRate, newPop;
  36. int years;
  37.  
  38. cout << fixed << setprecision(0);
  39.  
  40. // ---------------------- INPUT ----------------------
  41. cout << "Enter the starting population size: ";
  42. cin >> startPop;
  43. while (cin.fail() || startPop < 2)
  44. {
  45. cin.clear();
  46. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  47. cout << "ERROR: Starting population must be at least 2. Re-enter: ";
  48. cin >> startPop;
  49. }
  50.  
  51. cout << "Enter the annual birth rate (as a decimal, e.g., 0.03 for 3%): ";
  52. cin >> birthRate;
  53. while (cin.fail() || birthRate < 0)
  54. {
  55. cin.clear();
  56. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  57. cout << "ERROR: Birth rate cannot be negative. Re-enter: ";
  58. cin >> birthRate;
  59. }
  60.  
  61. cout << "Enter the annual death rate (as a decimal, e.g., 0.01 for 1%): ";
  62. cin >> deathRate;
  63. while (cin.fail() || deathRate < 0)
  64. {
  65. cin.clear();
  66. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  67. cout << "ERROR: Death rate cannot be negative. Re-enter: ";
  68. cin >> deathRate;
  69. }
  70.  
  71. cout << "Enter the number of years to project: ";
  72. cin >> years;
  73. while (cin.fail() || years < 1)
  74. {
  75. cin.clear();
  76. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  77. cout << "ERROR: Number of years must be at least 1. Re-enter: ";
  78. cin >> years;
  79. }
  80.  
  81. // -------------------- PROCESSING -------------------
  82. cout << "\nPopulation Projection:\n";
  83. cout << "------------------------------\n";
  84. newPop = startPop;
  85.  
  86. for (int i = 1; i <= years; i++)
  87. {
  88. newPop = calcPopulation(newPop, birthRate, deathRate);
  89. cout << "Year " << setw(2) << i << ": " << newPop << endl;
  90. }
  91.  
  92. return 0;
  93. }
  94.  
  95. // ---------------- Function Definition ----------------
  96. double calcPopulation(double prevPop, double birthRate, double deathRate)
  97. {
  98. // Formula: N = P + (B * P) - (D * P)
  99. return prevPop + (birthRate * prevPop) - (deathRate * prevPop);
  100. }
  101.  
Success #stdin #stdout 0.01s 5276KB
stdin
100
0.03
0.01
5
stdout
Enter the starting population size: Enter the annual birth rate (as a decimal, e.g., 0.03 for 3%): Enter the annual death rate (as a decimal, e.g., 0.01 for 1%): Enter the number of years to project: 
Population Projection:
------------------------------
Year  1: 102
Year  2: 104
Year  3: 106
Year  4: 108
Year  5: 110