fork download
  1. //Cameron Pham CS1A Chapter 6, p. 373, #15
  2. //
  3. /******************************************************************************
  4. * CALCULATE POPULATION SIZE
  5. * ______________________________________________________________________________
  6. * This program accepts an integer value for the starting size of a population,
  7. * number of years, and floating point values for the annual birth and death
  8. * rate.
  9. * ______________________________________________________________________________
  10. * INPUT
  11. * Population size // Starting size of population
  12. * Annual birth rate // Yearly rate at which people are born
  13. * Annual death rate // Yearly rate at which people pass away
  14. * Years // Period of time to calculate population
  15. *
  16. * OUTPUT
  17. * Size of population // Calculated size of a population after given years
  18. *
  19. *******************************************************************************/
  20. #include <iostream>
  21. #include <iomanip>
  22. using namespace std;
  23.  
  24. // Function prototype
  25. double calculatePopulation(double population, double birthRate,
  26. double deathRate);
  27.  
  28. int main()
  29. {
  30. // Declare variables
  31. double population;
  32. double birthRate;
  33. double deathRate;
  34. int years;
  35.  
  36. // Get the starting population
  37. cout << "Enter the starting population size: ";
  38. cin >> population;
  39. cout << endl;
  40.  
  41. // Validate the starting population
  42. while (population < 2)
  43. {
  44. cout << "Population must be at least 2. Enter again: ";
  45. cin >> population;
  46. }
  47.  
  48. // Get the annual birth and death rates
  49. cout << "Enter the annual birth rate (percentage): ";
  50. cin >> birthRate;
  51. cout << endl;
  52.  
  53. cout << "Enter the annual death rate (percentage): ";
  54. cin >> deathRate;
  55. cout << endl;
  56.  
  57. // Get the number of years
  58. cout << "Enter the number of years to display: ";
  59. cin >> years;
  60. cout << endl;
  61.  
  62. // Validate the number of years
  63. while (years < 1)
  64. {
  65. cout << "Years must be at least 1. Enter again: ";
  66. cin >> years;
  67. }
  68.  
  69. // Convert percentages to decimal rates
  70. birthRate /= 100.0;
  71. deathRate /= 100.0;
  72.  
  73. // Display the population
  74.  
  75. cout << fixed << setprecision(2);
  76. population = calculatePopulation(population, birthRate, deathRate);
  77. cout << years << " year(s): " << population << " people." << endl;
  78.  
  79.  
  80. return 0;
  81. }
  82.  
  83. // Function to calculate the new population size
  84. double calculatePopulation(double population, double birthRate,
  85. double deathRate)
  86. {
  87. double newPopulation;
  88.  
  89. newPopulation = population + birthRate * population - deathRate * population;
  90.  
  91. return newPopulation;
  92. }
Success #stdin #stdout 0s 5320KB
stdin
5000 0.80 0.90 10
stdout
Enter the starting population size: 
Enter the annual birth rate (percentage): 
Enter the annual death rate (percentage): 
Enter the number of years to display: 
10 year(s): 4995.00 people.