fork download
  1. // Timothy Price CS1A Ch5 Challenge 11
  2. /*******************************************************************************
  3.  * Calculate population
  4.  *
  5.  * This program receives values and calculates projected growth.
  6.  *
  7.  * Inputs:
  8.  * Statrting number of organisms
  9.  * Average daily growth percentage
  10.  * Number of days population will grow
  11.  *
  12.  * Outputs:
  13.  * Table displaying population size per day
  14.  ******************************************************************************/
  15.  
  16. #include <iostream>
  17. #include <iomanip>
  18.  
  19. using namespace std;
  20.  
  21. int main() {
  22.  
  23. // Data dictionary:
  24. int days;
  25. double dailypercent;
  26. double population;
  27. double increaserate;
  28.  
  29. cout << "Enter the starting number of organisms (minimum 2): ";
  30. cin >> population;
  31. cout << population << " organisms." << endl;
  32.  
  33. cout << "Enter the average daily population growth percentage: ";
  34. cin >> dailypercent;
  35. cout << dailypercent << "%" << endl;
  36.  
  37. cout << "Enter the number of days they will multiply (minimum 1): ";
  38. cin >> days;
  39. cout << days << " days." << endl;
  40.  
  41. // Input validation:
  42. if (population < 2)
  43. cout << "Invalid input. Starting size must be at least 2: ";
  44.  
  45. if (dailypercent < 0)
  46. cout << "Invalid input. Daily increase cannot be negative: ";
  47.  
  48.  
  49. if (days < 1)
  50. cout << "Invalid input. Number of days must be at least 1: ";
  51.  
  52.  
  53. // Change percentage to decimal value:
  54. increaserate = dailypercent / 100.0;
  55. if (population >= 2 && dailypercent >= 0 && days >= 1) {
  56. // Display table header:
  57. cout << "\n--- Population Growth Projection ---" << endl;
  58. cout << "Day\t\t\tPopulation Size" << endl;
  59. cout << "-----------------------------------" << endl;
  60.  
  61. // Display population for each day:
  62. for (int day = 1; day <= days; ++day) {
  63.  
  64. // Used static_cast to truncate the decimal value to find whole number:
  65. cout << day << "\t\t\t" << static_cast<int>(population) << endl;
  66.  
  67. // Calculate the population for the next day
  68. population += (population * increaserate);
  69. }
  70. }
  71. return 0;
  72. }
Success #stdin #stdout 0s 5312KB
stdin
10
25
30

stdout
Enter the starting number of organisms (minimum 2): 10 organisms.
Enter the average daily population growth percentage: 25%
Enter the number of days they will multiply (minimum 1): 30 days.

--- Population Growth Projection ---
Day			Population Size
-----------------------------------
1			10
2			12
3			15
4			19
5			24
6			30
7			38
8			47
9			59
10			74
11			93
12			116
13			145
14			181
15			227
16			284
17			355
18			444
19			555
20			693
21			867
22			1084
23			1355
24			1694
25			2117
26			2646
27			3308
28			4135
29			5169
30			6462