fork download
  1. //Amari Mosley CSC5 Chapter 6, P. 373, #15
  2. //
  3. /*****************************************************************
  4.  *
  5.  * Population Calculation
  6.  * _______________________________________________________________
  7.  * This program displays the size of a population for any number
  8.  * of years, based on a starting size, birth rate, and death rate.
  9.  *
  10.  * Computation is based on the formula:
  11.  * N = P + (B * P) - (D * P)
  12.  * where N = new population, P = previous population,
  13.  * B = birth rate, D = death rate
  14.  * _______________________________________________________________
  15.  * INPUT
  16.  * startSize : starting population size
  17.  * birthRate : annual birth rate as a percentage
  18.  * deathRate : annual death rate as a percentage
  19.  * years : number of years to display
  20.  *
  21.  * OUTPUT
  22.  * The population size for each year
  23.  *
  24.  *****************************************************************/
  25. #include <iostream>
  26. #include <iomanip>
  27. using namespace std;
  28.  
  29. // Function prototype
  30. double calcPopulation(double prevPop, double birthRate, double deathRate);
  31.  
  32. int main()
  33. {
  34. double startSize; // starting population size
  35. double birthRate; // annual birth rate
  36. double deathRate; // annual death rate
  37. int years; // number of years to display
  38. double population; // population size for the current year
  39.  
  40. // Get starting size with input validation
  41. cout << "Enter the starting size of the population: ";
  42. cin >> startSize;
  43. while (startSize < 2)
  44. {
  45. cout << "Starting size must be at least 2. Please re-enter: ";
  46. cin >> startSize;
  47. }
  48.  
  49. // Get birth rate with input validation
  50. cout << "Enter the annual birth rate (as a percentage): ";
  51. cin >> birthRate;
  52. while (birthRate < 0)
  53. {
  54. cout << "Birth rate cannot be negative. Please re-enter: ";
  55. cin >> birthRate;
  56. }
  57.  
  58. // Get death rate with input validation
  59. cout << "Enter the annual death rate (as a percentage): ";
  60. cin >> deathRate;
  61. while (deathRate < 0)
  62. {
  63. cout << "Death rate cannot be negative. Please re-enter: ";
  64. cin >> deathRate;
  65. }
  66.  
  67. // Get number of years with input validation
  68. cout << "Enter the number of years to display: ";
  69. cin >> years;
  70. while (years < 1)
  71. {
  72. cout << "Number of years must be at least 1. Please re-enter: ";
  73. cin >> years;
  74. }
  75.  
  76. // Convert percentages to decimals
  77. birthRate = birthRate / 100;
  78. deathRate = deathRate / 100;
  79.  
  80. // Display the population for each year
  81. cout << fixed << setprecision(2);
  82. cout << "\nYear\tPopulation" << endl;
  83. cout << "-----------------------" << endl;
  84.  
  85. population = startSize;
  86. for (int year = 1; year <= years; year++)
  87. {
  88. population = calcPopulation(population, birthRate, deathRate);
  89. cout << year << "\t" << population << endl;
  90. }
  91.  
  92. return 0;
  93. }
  94.  
  95.  
  96. // calcPopulation
  97. // Receives the previous population size, birth rate, and death
  98. // rate (as decimals) and returns the new population size.
  99. // Formula: N = P + BP - DP
  100.  
  101. double calcPopulation(double prevPop, double birthRate, double deathRate)
  102. {
  103. return prevPop + (birthRate * prevPop) - (deathRate * prevPop);
  104. }
Success #stdin #stdout 0s 5292KB
stdin
1000
3
1
3
stdout
Enter the starting size of the population: Enter the annual birth rate (as a percentage): Enter the annual death rate (as a percentage): Enter the number of years to display: 
Year	Population
-----------------------
1	1020.00
2	1040.40
3	1061.21