Monday, August 28, 2017

Programming Challenge 13.9 - Population

Example Files: Population.h
                          Population.cpp
                          PopulationDemo.cpp

Population.h


/* Population.h - Specification file of the Population class. */

#ifndef POPULATION_H
#define POPULATION_H

#include <string>

using std::string;

class Population
{
    private:
       string townName;                    // The name of a town
        double birthRate;                    // The number of births p.A.
        double deathRate;                    // The number of deaths p.A.
        double startingPopulation;        // The starting population
        int     numYears;
        double populationIncrease;        // Holds the population increase in percent
        double populationDecrease;        // Holds the population decrease in percent
        double populationNetGrowth;   // Holds the net population growth in percent

    public:
        Population()                        // Default constructor
        {
            townName = " ";
            birthRate = 0.0;
            deathRate = 0.0;
            populationIncrease = 0.0;
            populationDecrease = 0.0;
        }
      
        // Mutators
        void setTownName(string tName);
        void setNumYears(int nYears);
        void setStartingPopulation(double sPop);
        void setBirthRate(double bRate);
        void setDeathRate(double dRate);
        void calcPopulationIncrease(double sPop, double bRate, int numYrs);
        void calcPopulationDecrease(double sPop, double dRate, int numYrs);

        // Accessors
        string getTownName() const
        { return townName; }

        double getStartingPopulation() const
        { return startingPopulation; }

        double getBirthRate() const
        { return birthRate; }

        double getDeathRate() const
        { return deathRate; }

        double getPopulationIncrease() const
        { return populationIncrease / 100.0; }

        double getPopulationDecrease() const
        { return populationDecrease / 100.0; }

        int getNumYears() const
        { return numYears; }

        double getNetGrowth() const
        { return (populationIncrease / 100.0) - (populationDecrease / 100.0); }

        double getNewPopulation() const
        { return (populationIncrease - populationDecrease) + startingPopulation; }
};
#endif

Population.cpp


/* Population.cpp - Implementation file for the Population class */

#include <cmath>
#include "Population.h"

/* **********************************************************
            Population::setTownName
    Assigns a value to the setTownName member variable. If the
    field is not empty, the name passed to it is used, else a
    default name is assigned to it.
    ********************************************************** */

void Population::setTownName(string tName)
{
    if (!tName.empty())
    {
        townName = tName;
    }
    else
    {
        townName = "Terrania";
    }
}

/* **********************************************************
            Population::setStartingPopulation
    Assigns a value to the startingPopulation member variable.
    If the value is greater than 0, the value passed to it is
    used, else the variable is set to 1,000.
    ********************************************************** */
void Population::setStartingPopulation(double sPop)
{
    if (sPop > 0.0)
    {
        startingPopulation = sPop;
    }
    else
    {
        startingPopulation = 1000.0;
    }
}

/* **********************************************************
            Population::setBirthRate
    Assigns a value to the birthRate member variable. If the
    value is greater than 0, the value passed to it is used,
    else the variable is set to 2.48.
    ********************************************************** */

void Population::setBirthRate(double bRate)
{
    if (bRate > 0.0 && bRate <= 100.0)
    {
        birthRate = bRate;
    }
    else
    {
        birthRate = 2.48;
    }
}

/* **********************************************************
            Population::setDeathRate
    Assigns a value to the deathRate member variable. If the
    value is greater than 0 and lower than 100, the value
    passed to it is used, else the variable is set to 1.15.
    ********************************************************** */

void Population::setDeathRate(double dRate)
{
    if (dRate > 0.0 && dRate <= 100.0)
    {
        deathRate = dRate;
    }
    else
    {
        deathRate = 1.15;
    }
}

/* **********************************************************
            Population::setNumYears
    Assigns a value to the numYears member variable. If the
    value is greater 0, the value passed to it is used, else
    the variable is assigned a default value of 5.
   ********************************************************** */

void Population::setNumYears(int nYears)
{
    if (nYears > 0)
    {
        numYears = nYears;
    }
    else
    {
        numYears = 5;
    }
}

/* **********************************************************
            Population::calcPopulationIncrease
    Calculates the population increase taking place over a
    period of time.
   ********************************************************** */

void Population::calcPopulationIncrease(double sPop, double bRate, int numYrs)
{
    populationIncrease += bRate * (pow((1 + bRate / 100.0), numYrs) * 10);
}

/* **********************************************************
            Population::calcPopulationDecrease
    Calculates the population decrease taking place over a
    period of time.
    ********************************************************** */

void Population::calcPopulationDecrease(double sPop, double dRate, int numYrs)
{
    populationDecrease += dRate * (pow((1 - dRate / 100.0), numYrs) * 10);
}

PopulationDemo.cpp


 /* PopulationDemo.cpp - This program demonstrates the Population class. */

#include <string>
#include <iostream>
#include <iomanip>
#include "Population.h"

using std::cin;
using std::cout;
using std::fixed;
using std::showpoint;
using std::setprecision;

char tryAgain();
void getData(Population &);
void calcPopChange(const Population);
void displayStartData(const Population);
void displayPopChange(Population);

int main()
{
    Population populous;
  
    char again = ' ';

    cout << "POPULOUS - Population Statistics Calculator\n";

    do
    {
        getData(populous);
        displayStartData(populous);
        calcPopChange(populous);

        again = tryAgain();

        if (toupper(again) == 'N')
        {
            cout << "\nThank you for trying Populous! Have a nice day!";
        }

    } while (toupper(again) != 'N');

    cin.ignore();

    return 0;
}

/* **********************************************************
    getData (accepts a reference to a Circle object)
    The user is asked to input the following information:

        * The town name (if the user leaves this field blank,
          a default name is used)
        * The starting population (if the user enters 0, a
          default value of 1,000 is used)
        * The number of births (if the user enters 0, a default
          value of 2.48 is used)
        * The number of deaths (if the user enters 0, a default
          value of 1.15 is used)
        * The number of years to calucalate population change
          (If the user enters 0, a default value of 5 is used)
    ********************************************************** */

void getData(Population &populous)
{
    string tName = " ";
    double bRate = 0.0;
    double dRate = 0.0;
    double sPop = 0.0;
    int    numYrs = 0;

    cout << "\nEnter the name of your town: ";
    std::getline(cin, tName);
    populous.setTownName(tName);

    cout << "Enter the number of people currently living in your town: ";
    cin >> sPop;
    populous.setStartingPopulation(sPop);

    cout << "Enter the annual birth rate of your town [Ex.: 2.48%]: ";
    cin >> bRate;
    populous.setBirthRate(bRate);

    cout << "Enter the annual death rate of your town [Ex.: 1.15%]: ";
    cin >> dRate;
    populous.setDeathRate(dRate);

    cout << "Enter the number of years you wish the simulation to run: ";
    cin >> numYrs;
    populous.setNumYears(numYrs);
}

/* **********************************************************
   calcPopChange (accepts a Population object)
    This function calculates the population increase and
    decrease over a period of time.
   ********************************************************** */

void calcPopChange(Population populous)
{
    for (int i = 0; i < populous.getNumYears(); ++i)
    {
        populous.calcPopulationIncrease(populous.getStartingPopulation(), populous.getBirthRate(), i);
        populous.calcPopulationDecrease(populous.getStartingPopulation(), populous.getDeathRate(), i);

        displayPopChange(populous);
    }
}

/* **********************************************************
   displayStartData (accepts a const Population object)
    This function displays the initial values set by the user.
   ********************************************************** */

void displayStartData(const Population populous)
{
    cout << "\nWelcome to "      << populous.getTownName() << " Administrator!\n"
          << "This is the population you are starting with:\n"
          << "Number of people: " << populous.getStartingPopulation() << "\n"
          << "Number of births: " << populous.getBirthRate() << "% p.A.\n"
          << "Number of deaths: " << populous.getDeathRate() << "% p.A.\n";
}

/* **********************************************************
   displayPopChange (accepts a const Population object)
    This function displays the population change over a period
    of time.
   ********************************************************** */

void displayPopChange(Population populous)
{
    static int i = 0;

    cout << "\nIn year " <<  (i += 1)   << " the population of "
          << populous.getTownName()       << " has grown to "
          << populous.getNewPopulation() << " people.\n\n";

    cout << fixed << showpoint << setprecision(2);
    cout << "Birth Rate: " << populous.getPopulationIncrease() << "%\n";
    cout << "Death Rate: " << populous.getPopulationDecrease() << "%\n";
    cout << "Net Growth: " << populous.getNetGrowth()              << "%\n";
}

/* **********************************************************
    Definition: tryAgain

    This function asks the user if he or she wishes to try
    again. This decision is returned.
    ********************************************************** */

char tryAgain()
{
    char again = ' ';

    cout << "\nDo you wish to try this again? ";
    cin >> again;
    cin.ignore();

    /* Input validation */
    while (toupper(again) != 'Y' && toupper(again) != 'N')
    {
        cout << "\nDo you wish to try this again? ";
        cin >> again;
    }

    return toupper(again);
}

Example Output: 



No comments:

Post a Comment