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