Tuesday, December 13, 2016

Programming Challenge 5.11 - Population

/* Population - This program predicts the size of a population of organisms.

The program asks the user for the starting number of organisms, their
average daily population increase (as percentage), and the number of days
they will multiply. A loop will display the size of the population for
each day.

Input Validation: No numbers less than 2 are accepted for the starting
size of the population. No negative numbers for average daily population
increase is accepted. No numbers less than 1 for the number of days they
will multiply are accepted. */

#include "Utility.h"

int main()
{
    /* Variables: Days (counter variable), Number of days */
    int days = 0,
        numberDays = 0;

    /* Variables: Starting number of organisms, Average daily population
       increase, Population increase, Percentage of increase of population */
    float startingPopulation = 0,
          averageDailyIncrease = 0,
          populationIncrease = 0,
          pctPopulationIncrease = 0;

    /* Display: Introduction */
    cout << "\t\tCreatures Population Predicition Program\n\n"
        << "This program allows you to predict the growth in size of\n"
        << "your creatures population over a period of time. Please\n"
        << "provide the necessary data, and i will calculate the daily\n"
        << "increase for you.\n";

    /* Input: The starting population, Average daily population increase,
    Number of days */
    cout << "\nPlease enter the starting number of creatures (2 and up): ";
    cin >> startingPopulation;

    /* Input Valdiation: While the user enters a number less than 2, the
    loop will iterate */
    while (startingPopulation < 2)
    {
        /* Display: Error message */
        cout << "\nYou entered a number that is not valid. Please enter a\n"
            << "population size of no less than 2.";

        /* Ask the user again */
        cout << "Please enter the starting number of creatures (2 and up): ";
        cin >> startingPopulation;
    }

    cout << "\nBy how many percent (1 or above) are your creatures expected\n"
        << "to increase on average daily? % ";
    cin >> averageDailyIncrease;

    /* Input Validation: While the user enters a number less than 0, this
    loop will iterate */
    while (averageDailyIncrease <= 0)
    {
        /* Display: Error message */
        cout << "\nSorry, but you entered a value equal to or less than 0.\n"
            << "Only numbers above zero .1, .5 ... are allowed.\n";

        /* Ask the user again */
        cout << "By how many percent (1 or above) are your creatures expected\n"
            << "to increase on average daily? ";
        cin >> averageDailyIncrease;
    }

    cout << "\nPlease enter the number of days your creatures should multiply: ";
    cin >> numberDays;

    /* Input validation: While the number of days is less than 1, this loop
    will iterate */
    while (numberDays < 1)
    {
        /* Display: Error message */
        cout << "\nOh no, an error occured! You entered a number of days\n"
                "less than 1. Only a number of days of 1 or above is allowed.\n";

        /* Ask the user again */
        cout << "Please enter the number of days your creatures should multiply: ";
        cin >> numberDays;
    }

    /* Display: Table header, General information */
    cout << "\nGood News! Your creatures have been busy mating!\n"
        << "See how their population has grown while you were away.\n";

    cout << "\nNumber of days: " << "\t" << "Increase of population"
        << "\t\t\t\tPercent";

    /* For loop: As long as days is smaller than or equal to number of
       days entered by the user, this loop will iterate */
    for (days = 1; days <= numberDays; days++)
    {
        /* Nested While Loop::Display: While days is equal to 1, and days
           is smaller than number of days the user entered, this loop
           will iterate
        */
        while (days == 1 && days < numberDays)
        {  
            /* Display: General information about the beginning number of
               creatures, the number of days the will mate, and the average
               rate in percent at which their population grows */
            cout << "\nDay: " << (days) << "\t\t\t" << startingPopulation
                 << "\t\t\t\t\t\t" << "% 0.00\n";
            days++;
        }

        /* Calculations: Increase of population, Increase of percentage */
        startingPopulation = (startingPopulation * averageDailyIncrease);
        pctPopulationIncrease = (startingPopulation +
                                (averageDailyIncrease / 100) * startingPopulation);

        /* Display: Formatted output of the increase of population for
           each day */
        cout << fixed << showpoint << setprecision(2);
        cout << "Day: " << days << "\t\t\t" << setw(14) << left << startingPopulation
             << "\t\t\t\t\t" << "% " << setw(32) << left << pctPopulationIncrease
             << setw(5) << left << endl;
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment