fork download
  1. //Andres Guzman CSC5 Chapter 6, P. 373, #15
  2. //
  3. /*******************************************************************************
  4. *
  5. * CALCULATE POPULATION
  6. * ______________________________________________________________________________
  7. * This program will calculate a population after an inputed set of years
  8. *
  9. * Computation:
  10. *
  11. * N = P + BP - BP
  12. * ______________________________________________________________________________
  13. * INPUT
  14. *
  15. * pop = Input for population
  16. * birth = Input for birth rate
  17. * death = Input for death rate
  18. * years = Input for number of years
  19. * OUTPUT
  20. * newpop = New population
  21. *******************************************************************************/
  22. #include <iostream>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. int pop; //Global Variable
  27. double birth; //Global Variable
  28. double death; //Global Variable
  29. int years; //Global Variable
  30.  
  31. double population(); //function prototype
  32.  
  33. int main ()
  34. {
  35. cout << "\tPopulation" << endl;
  36. cout << "Population Starting Size: ";
  37. cin >> pop;
  38. //
  39. //While loop to validate population input
  40. while(pop < 2) //pop cannot be less than 2
  41. {
  42. cout << "Invalid Starting Size." << endl;
  43. cout << "Population Starting Size: ";
  44. cin >> pop;
  45. }
  46. cout << "\nAnnual Birth Rate: ";
  47. cin >> birth;
  48. //
  49. //While loop to validate birth rate input
  50. while (birth < 0) //birth cannot be less than 0
  51. {
  52. cout << "Invalid Rate." << endl;
  53. cout << "Annual Birth Rate: ";
  54. cin >> birth;
  55. }
  56. cout << "\nAnnual Death Rate: ";
  57. cin >> death;
  58. //
  59. //While loop to validate death rate input
  60. while (death < 0) //death cannot be less than 0
  61. {
  62. cout << "Invalid Rate." << endl;
  63. cout << "Annual Death Rate: ";
  64. cin >> death;
  65. }
  66. cout << "\nYears to Display: ";
  67. cin >> years;
  68. cout << endl;
  69. //
  70. //While loop to validate years input
  71. while (years < 1) // Years cannot be less than 1
  72. {
  73. cout << "Invalid Years." << endl;
  74. cout << "Years to Display: ";
  75. cin >> years;
  76. cout << endl;
  77. }
  78. //
  79. //Calling function
  80. population(); //Function Call
  81. }
  82.  
  83. /*******************************************************************************
  84.  * Creating defintion for function population. It will use the inputs from the
  85.  * global variables to caluclate the new population using the formula inside and
  86.  * then will output a loop with the result for each year.
  87.  ******************************************************************************/
  88. double population()
  89. {
  90. double newpop = pop; //Transfers pop value to newpop for easier
  91. //calculation.
  92.  
  93. for (int i = 1; i <= years; i++) //For loop to loop each inputed year
  94. {
  95. newpop = newpop + (birth * newpop) - (death * newpop); //formula for
  96. //new population
  97. //
  98. //If statement to remove any negative results
  99. if(newpop < 0) //
  100. {
  101. newpop = 0; //If its less than 0, then the value becomes 0
  102. }
  103. //
  104. //Outputting results and allowing for decimal points
  105. cout << fixed << setprecision(2) << "Year " << i << ": ";
  106. cout << newpop << endl;
  107. }
  108. return newpop; //returns the value of newpop
  109. }
Success #stdin #stdout 0s 5324KB
stdin
2 2 2 2
stdout
	Population
Population Starting Size: 
Annual Birth Rate: 
Annual Death Rate: 
Years to Display: 
Year 1: 2.00
Year 2: 2.00