fork download
  1. //Castulo Jason Quintero CSC5 Chapter 6, Pg. 373, #15
  2. //
  3. /**************************************************************************
  4.  *
  5.  * Calculate Population Size
  6.  * ________________________________________________________________________
  7.  * This program recieves as user input the current population
  8.  * size, annual birth rate, annual death rate, and years
  9.  * requested to calculate the new population size each year.
  10.  *
  11.  * Computation is based on formula:
  12.  * newPopulationsize = previousPopulationsize +
  13.  * (birthRate * previousPopulationsize) -
  14.  * (deathRate * previousPopulationsize)
  15.  * ________________________________________________________________________
  16.  * INPUT
  17.  * previousPopulationsize : Previous population size
  18.  * birthRate : Annual birth rate percentage
  19.  * deathRate : Annual death rate percentage
  20.  * year : Years requesting to be displayed
  21.  *
  22.  * OUTPUT
  23.  * year : Year being displayed with loop
  24.  * newPopulationsize : New size of population each year
  25.  *************************************************************************/
  26.  
  27. #include <iostream>
  28. #include <iomanip>
  29. using namespace std;
  30.  
  31. // Prototype
  32. float popSize(float, float, float);
  33.  
  34. // Main function
  35. int main()
  36. {
  37. float newPopulationsize; // New population size
  38. float previousPopulationsize = 0; // Previous population size
  39. float birthRate; // Annual birth rate percentage
  40. float deathRate; // Annual death rate percentage
  41. float year; // Years requesting to be displayed
  42.  
  43. // Input for starting population
  44. cout << "Please enter the starting size of the population: \n";
  45. cin >> previousPopulationsize;
  46.  
  47. // Input validation
  48. while (previousPopulationsize < 2)
  49. {
  50. cout << "Please enter a starting population that is atleast "
  51. << "two people. \n";
  52. cin >> previousPopulationsize;
  53. }
  54.  
  55. // Input annual birth rate percentage
  56. cout << "Please enter the annual birth rate percentage: \n";
  57. cin >> birthRate;
  58.  
  59. // Input validation
  60. while (birthRate < 0)
  61. {
  62. cout << "Please enter a non negative number for the birth rate \n";
  63. cin >> birthRate;
  64. }
  65.  
  66. // Input annual death rate percentage
  67. cout << "Please enter the annual death rate percentage: \n";
  68. cin >> deathRate;
  69.  
  70. // Input validation
  71. while (deathRate < 0)
  72. {
  73. cout << "Please enter a non negative number for the birth rate \n";
  74. cin >> deathRate;
  75. }
  76.  
  77. // Input years to be displayed
  78. cout << "Please enter the number of years to be displayed: \n";
  79. cin >> year;
  80.  
  81. // Input validation
  82. while (year < 1)
  83. {
  84. cout << "Please enter a value greater than 1 for the years to "
  85. << "be displayed. \n";
  86. cin >> year;
  87. }
  88.  
  89. // For loop to give value for each year
  90. for (int i = 1; i <= year; i++)
  91. {
  92. newPopulationsize = popSize(previousPopulationsize, birthRate,
  93. deathRate);
  94. previousPopulationsize = newPopulationsize;
  95.  
  96. cout << setprecision(0) << fixed;
  97. cout << "The population for year " << i << " was "
  98. << newPopulationsize << endl;
  99. }
  100. return 0;
  101. }
  102.  
  103. // Value return function and calculations
  104. float popSize (float P, float B, float D)
  105. {
  106. float N;
  107. B *= .01; // Converts to percentage
  108. D *= .01; // Converts to percentage
  109. N = P + (B * P) - (D * P); // Formual to get new population
  110. return N; // Return value of N
  111. }
Success #stdin #stdout 0s 5304KB
stdin
100 10 5 5
stdout
Please enter the starting size of the population: 
Please enter the annual birth rate percentage: 
Please enter the annual death rate percentage: 
Please enter the number of years to be displayed: 
The population for year 1 was 105
The population for year 2 was 110
The population for year 3 was 116
The population for year 4 was 122
The population for year 5 was 128