fork download
  1. //Devin Scheu CS1A Chapter 6, P. 173, #15
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE POPULATION GROWTH OVER YEARS
  6. * ____________________________________________________________
  7. * This program determines the population size over a specified
  8. * number of years based on starting size, birth rate, and death rate.
  9. * ____________________________________________________________
  10. * INPUT
  11. * startingPopulation : The initial population size
  12. * birthRate : The annual birth rate percentage
  13. * deathRate : The annual death rate percentage
  14. * numYears : The number of years to calculate
  15. *
  16. * OUTPUT
  17. * populationTable : The population size for each year
  18. *
  19. **************************************************************/
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23.  
  24. using namespace std;
  25.  
  26. // Function to calculate population for a year
  27. double calculatePopulation(double previousPopulation, double birthRate, double deathRate) {
  28. double births = previousPopulation * (birthRate / 100);
  29. double deaths = previousPopulation * (deathRate / 100);
  30. return previousPopulation + births - deaths;
  31. }
  32.  
  33. int main () {
  34.  
  35. //Variable Declarations
  36. double startingPopulation; //INPUT - The initial population size
  37. double birthRate; //INPUT - The annual birth rate percentage
  38. double deathRate; //INPUT - The annual death rate percentage
  39. int numYears; //INPUT - The number of years to calculate
  40. double currentPopulation; //OUTPUT - The population size for each year
  41. string populationTable; //OUTPUT - The population size for each year
  42.  
  43. //Prompt for Input and Echo
  44. cout << "Enter the starting population size: ";
  45. cin >> startingPopulation;
  46. while (startingPopulation < 2) {
  47. cout << "\nError: Please enter a number greater than or equal to 2: ";
  48. cin >> startingPopulation;
  49. }
  50. cout << startingPopulation << endl;
  51.  
  52. cout << "Enter the annual birth rate (%): ";
  53. cin >> birthRate;
  54. while (birthRate < 0) {
  55. cout << "\nError: Please enter a non-negative birth rate: ";
  56. cin >> birthRate;
  57. }
  58. cout << birthRate << "%" << endl;
  59.  
  60. cout << "Enter the annual death rate (%): ";
  61. cin >> deathRate;
  62. while (deathRate < 0) {
  63. cout << "\nError: Please enter a non-negative death rate: ";
  64. cin >> deathRate;
  65. }
  66. cout << deathRate << "%" << endl;
  67.  
  68. cout << "Enter the number of years: ";
  69. cin >> numYears;
  70. while (numYears < 1) {
  71. cout << "\nError: Please enter a number greater than or equal to 1: ";
  72. cin >> numYears;
  73. }
  74. cout << numYears << endl;
  75.  
  76. //Separator and Output Section
  77. cout << "-------------------------------------------------------" << endl;
  78. cout << "OUTPUT:" << endl;
  79.  
  80. //Display Header
  81. cout << fixed << setprecision(0);
  82. cout << left << setw(15) << "Year" << right << setw(20) << "Population" << endl;
  83. cout << "-----------------------------------------------" << endl;
  84.  
  85. //Calculate and Display Population for Each Year
  86. currentPopulation = startingPopulation;
  87. for (int year = 0; year <= numYears; year++) {
  88. if (year == 0) {
  89. cout << left << setw(15) << "Starting" << right << setw(20) << static_cast<long long>(currentPopulation) << endl;
  90. } else {
  91. currentPopulation = calculatePopulation(currentPopulation, birthRate, deathRate);
  92. cout << left << setw(15) << year << right << setw(20) << static_cast<long long>(currentPopulation) << endl;
  93. }
  94. }
  95.  
  96. } //end of main()
Success #stdin #stdout 0s 5308KB
stdin
100000
3
5
5
stdout
Enter the starting population size: 100000
Enter the annual birth rate (%): 3%
Enter the annual death rate (%): 5%
Enter the number of years: 5
-------------------------------------------------------
OUTPUT:
Year                     Population
-----------------------------------------------
Starting                     100000
1                             98000
2                             96040
3                             94119
4                             92236
5                             90392