Monday, December 26, 2016

Programming Challenge 5.18 - Population Bar Chart

Example File: People.txt

 /* Population Bar Chart - This program produces a bar chart showing the
   population growth of Prairieville, a small town in the Midwest, at
   20-year intervals during the past 100 years.
  
   The program reads in the population figures (rounded to the nearest
   1,000 people) for 1900, 1920, 1940, 1960, 1980 and 2000 from a file.
  
   For each year it displays the date and a bar consisting of one asterisk
   for each 1,000 people. The data can be found, and is retrieved from,
   People.txt.
  
   Example output:

   PRAIRIEVILLE POPULATION GROWTH
   (each * represents 1,000 people)
   1900  **
   1290  ****
   1940  ***** */

#include "Utility.h"

int main()
{
    /* Declare: File stream variable */
    ifstream populationData;

    /* Variables: Date, Population growth, Rounding to nearest thousand,
       Population chart */
    int date = 0,
        populationGrowth = 0,
        roundingToNearest = 0,
        popChart = 0;

    /* Open: File "People.txt" */
    populationData.open("People.txt");

    /* If Statement::Error handling: If the file "People.txt" was opened
       successfully, this statement will be executed */
    if (populationData)
    {
        /* Display: Bar chart header */
        cout << "PRAIRIEVILLE POPULATION GROWTH\n"
            << "(each * represents 1,000 people)\n";

        /* While Loop: While date is lower than or euqal to 2000, this
           loop will itereate */
        while (date <= 2000)
        {
            /* Read-In::First set of Data: Date, Population growth data */
            populationData >> date >> populationGrowth;

            /* Calculations: To get to the nearest 1000, the remainder
               of each set of population growth data is determined, and
               the remainder then is subtracted from population growth */
                roundingToNearest = (populationGrowth % 1000);
                populationGrowth -= roundingToNearest;

                /* If Statement: If the remainder is greater than or
                   equal to 945, this statement will be executed, and
                   population growth is rounded by adding +1000 to
                   itself */
                if (roundingToNearest % 1000 >= 945)
                {
                    populationGrowth += 1000;
                }

                /* Calculation: For displaying the population growth data
                   equivalent in asterisk, population growth is divided
                   by 1000 */
                populationGrowth /= 1000;

                /* Display: Date */
                cout << "" << date << "  ";

            /* For loop: As long as popChart is smaller than, or equal to,
               populationGrowth, this loop will iterate */
            for (int popChart = 1; popChart <= populationGrowth; popChart++)
            {
                cout << '*';
            }
            cout << endl;

            /* Read-In::Next set of Data: Date (date +20), population
            growth data */
            populationData >> date >> populationGrowth;
        }
    }
    else
    {
        /* Display: Error message, if file could not be opened */
        cout << "A file openening error has occured. Please make sure that\n"
            << "the specified file is not damaged, has been accidentally\n"
            << "deleted, and that the file is located in the program folder.\n"
            << "Please close the program and try again.\n";
    }

    /* Close: "People.txt" */
    populationData.close();

    pauseSystem();
    return 0;
}

No comments:

Post a Comment