Friday, March 26, 2010

Population Bar Chart: Processes the population figures so that it's one symbol ('\x03') represents 1000 people and displays the year and it's consequent population in terms of neatly calculated symbol.


// Programming Assignment: Population Bar Chart
// Programming Environment: Bloodshed
// Date/Time: 2/10/2010 8:31:19 PM
//------------------------------------------------------------------------------
// This program takes input from "prople.dat" file from external storage device.
// The inputs are population of small town. It processes the population figures
// so that it's one symbol ('\x03') represents 1000 people and displays the year
// and it's consequent population in terms of neatly calculated symbol.
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//---------------------------------------------------------------------------
const char BAR_CHART_SYMBOL = '\x03'; // Love symbol for bar representation.
const char MY_PATH_FILE[]="c:\\c++\\assignment 4\\people.dat";
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int main (void)
{
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);

int population;
int no_symbol;
int year = 1900; // Initialization of variable "year" to 1900.

// Reading the population figures for the years(interval of 20 years).
ifstream My_Infile;
My_Infile.open(MY_PATH_FILE);
if(My_Infile){
cout << endl << "File from external source opened: Successfully!"; // Formatting the o/p so that it would look good on screen! cout << endl << endl << "PRAIRIEVILLE POPULATION GROWTH"; cout << endl << "[Each \x03 represents 1,000 people]" << endl; cout << endl << "YEAR" << " " << "POPULATION"; cout << endl << "----" << " " << "----------"; // Reading one line of data at a time and performing calculations and // displaying the population in terms of symbols. while (My_Infile >> population ) {
//My_Infile >> population;
cout << endl << year << setw(8); no_symbol = population / 1000; // If resudial population is greater or equal to 500 then // considering it as 1000 & incrementing symbol by 1. if (population % 1000 >= 500) {
no_symbol ++;
}

// Using for loop to display the symbol.
for (int count = 1; count <= no_symbol; ++count) {
cout << BAR_CHART_SYMBOL;
}

// Population Parentheses.
cout << setw(2) << "[" << population << "]";
year = year + 20; // Incrementing year by 20.
}

My_Infile.close(); // Closing the file as it would be unnecessary at
// this point of the program.
} else {
cout << endl << "Error: File Open Error!"; // Error message if the file
// don't open!
}

cout << endl << endl << endl << "Depress Any Key to Continue...";
_getch ();
return 0;
}
//---------------------------------------------------------------------------
// Function Definitions follow.
//---------------------------------------------------------------------------

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.