Wednesday, January 11, 2017

Programming Challenge 6.16 - Population

/* Population - In a population, the birth rate is the percentage
   increase of the population due to births, and the death rate is
   the percentage decrease of the population due to deaths. This
   program displays the size of a population for any number of years.
   The program asks for the following data:
  
   * The starting size of the population
   * The annual birth rate
   * The annual death rate
   * The number of years to display

   This program uses a function that calculates the size of the
   population for a year. The formula is:

   N = P + BP - DP

   * N is the new population size
   * P is the previous population size
   * B is the birth rate
   * D is the death rate

   The second function is used to display an introduction to this
   program.

   Input Validation: No numbers less than 2 for the starting size, no
   negative numbers for birth rate or death rate, and no numbers less
   than 1 for the number of years is accepted. */

#include "Utility.h"

/* Prototype: Population growth, Show Introduction */
long double populationGrowth(long double, long double, long double, int);
void showIntroduction();

int main()
{
   /* Constants: Begin, Again, Quit */
   const int BEGIN = 1,
             AGAIN = 2,
             QUIT = 3;

   /* Variables: Population start size, Annual birth rate,
                 Annual death rate, Total growth of population */
   double popStartSize = 0.0,
          annBirthRate = 0.0,
          annDeathRate = 0.0,
          popGrowthTotal = 0.0;

   /* Call: showIntroduction */
   showIntroduction();

   /* Variables: Number of years, Menu selection */
   int numberYears = 0,
       selection = 0;
  
   /* Display: Menu
      Get: Input for the size of the starting population,
           the annual birth rate, the annual death rate,
           the number of years the user wishes to aquire
           data for
      Validate: Input, Menu selection */
   do
   {
      cout << "1. Begin\n"
           << "2. Try again?\n"
           << "3. Quit\n\n"
           << "Please make your selection: ";
      cin >> selection;

      while (selection < 1 || selection > 3)
      {
         cout << "Your selection could not be processed.\n"
              << "Please select (1) to begin, (2) to enter\n"
              << "another set of data, or (3) to quit.\n"
              << "Please make your selection: ";
         cin >> selection;
      }

      if (selection != QUIT)
      {
         cout << "\nWhat is the starting size of the population? ";
         cin >> popStartSize;

         /* While PopStartSize is lower than 2, the user is asked to repeat
            his input    */
         while (popStartSize < 2)
         {
            cout << "\nSorry, any number below 2 for your population's starting\n"
                 << "size can not be accepted. Please repeat your input: ";
            cin >> popStartSize;
         }

         cout << "What is the annual birth rate of the population? % ";
         cin >> annBirthRate;

         while (annBirthRate < 0)
         {
            cout << "\nYour populations birth rate can not be 0 or negative. It\n"
                    "has to be a positive value, for example 2.48%, or above.\n"
                    "Please repeat your input: ";
            cin >> annBirthRate;
         }

         cout << "What is the annual death rate of the population? % ";
         cin >> annDeathRate;

         while (annDeathRate < 0)
         {
            cout << "\nYour populations death rate can not be 0 or negative. A\n"
               << "value of 1.15% and above is the least you should enter as\n"
               << "a starting value. Please repeat your input: ";
            cin >> annDeathRate;
         }

         cout << "How many years should be displayed? ";
         cin >> numberYears;

         while (numberYears < 0)
         {
            cout << "\nThe number of years you entered was 0 or below 0."
                 << "Please repeat your input: ";
            cin >> numberYears;
         }

         /* Call: populationGrowth */
         popGrowthTotal = populationGrowth(popStartSize, annBirthRate,
                          annDeathRate, numberYears);

         /* Display: The final result of the calculations done in
                     populationGrowth, the return value is demoted
                     to an int to get a round number, and popGrowthTotal
                     is converted to a %-value */
         cout << "\nAfter " << numberYears
            << " years, the population grew to a size of "
            << ((int)popGrowthTotal + popStartSize)
            << " or a total of % " << (popGrowthTotal / 100)
            << ".\n\n";
      }

      if (selection == QUIT)
      {
         cout << "\nProgram shutting down ..."
              << "(Press enter to close the window)\n";
         cin.get();
      }
   } while (selection != QUIT);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: showIntroduction

   This function displays information about this program
   ********************************************************** */

void showIntroduction()
{
   cout << "\t\tNatural Population Growth Simulator\n\n"
        << "This program simulates the workings of a natural\n"
        << "growth formula, in an ideal environment, without\n"
        << "outside factors that influence the birth and death\n"
        << "rates. Here is an example input:\n\n"
        << " * Size of starting population: 1000.0\n"
        << " * Annual birth rate: %2.48 (or 248 births)\n"
        << " * Annual death rate: %1.15 (or 115 deaths)\n"
        << " * Period of time: 10 (years)\n\n"
        << "This results in a total of 1074 new inhabitants\n"
        << "or %0.74 increase after 5 years.\n\n"
        << "You can experiment with the values to get different\n"
        << "results. For instance, start with 2 people, and have\n"
        << "a very low birth, or a very high birth and low death\n"
        << "rate, that leads to hundreds of millions of new\n"
        << "inhabitants in a very short amount of time!\n\n";
}

/* **********************************************************
   Definition: populationSize

   This function accepts four parameters as arguments:

   * Population starting size
   * Annual birth rate
   * Annual death rate
   * Number of years

   It calculates the birth rate, death rate, and natural
   growth rate of a population under the assumption that
   both increase and decrease stay the same over a period
   of time.

   After the calculations the preliminary results are shown
   on screen, and the total net growth rate, e.g. births -
   deaths is passed back to main to display the final result.
   ********************************************************** */

long double populationGrowth(long double startingPopulation, long double birthRate,
   long double deathRate, int numYears)
{
   /* Variables: Projected birth rate, Projected Death rate, Natural increase rate
                 in percent, Natural decrease rate, Population increase,
                 Population decrease, Natural growth rate total */
   long double pjtBirthRate = birthRate,
               pjtDeathRate = deathRate,
               populationIncrease = 0.0,
               populationDecrease = 0.0,
               natGrowthTotal = 0.0;

   cout << fixed << showpoint << setprecision(2);

   /* Display: Table header */
   cout << "\nPopulation Increase" << "\t "
      << " % " << "\t "
      << "Population Decrease" << "\t "
      << "  % " << "\t "
      << "Net Population Increase" << "\t"
      << " % " << endl;

   cout << "............................................................."
        << ".....................................\n";

   /* The growth and decay rates are calculated as separate entities,
      so as to have a better understanding by how much the population
      increases and decreases in both decimal numbers and percentage
      values, as well as the total Increase, births and deaths, also
      as percentage and decimal values */
   for (int yearsPassed = 0; yearsPassed < numYears; ++yearsPassed)
   {
      populationIncrease += pjtBirthRate *
         (pow((1 + pjtBirthRate / 100.0), yearsPassed) * 10.0);

      populationDecrease += pjtDeathRate *
         (pow((1 - pjtDeathRate / 100.0), yearsPassed) * 10.0);

      natGrowthTotal = populationIncrease - populationDecrease;

      /* Display: Output while the loop iterates */
      cout << setw(19) << right << populationIncrease + startingPopulation
           << setw(8) << right << (populationIncrease / 100)
           << setw(25) << right << -populationDecrease
           << setw(8) << right << (populationDecrease / 100)
           << setw(12) << right << (natGrowthTotal + startingPopulation)
           << setw(26) << right << (natGrowthTotal / 100) << endl;
   }

   pauseSystem();

   /* Return: The natural growth rate */
   return natGrowthTotal;
}

Example Output:




No comments:

Post a Comment