Homework 13: Simple Stats
This assignment is designed to exercise reading multiple pieces of data from a file, a "row at a time."
Concepts
Focus on one main concept in this assignment: properly extracting rows of data from a text-based input file.
Extracting "rows" of Data
Think about all the tables of data you've seen in your life. They're generally made up of columns and rows:
Code | Age | Breakups |
---|---|---|
O | 876 | 394.4 |
M | 8376 | 3944.4 |
F | 8765 | 3946.4 |
G | 8776 | 394.3 |
Let's assume you've been given a bunch of tabular data that looks like this:
M 8376 3944.4
F 8765 394.4
G 8776 394.3
Any time you wish to extract primitive data a row at a time, yet still be able to do something with each individual piece of data in the row, you should follow this sort of pattern:
using namespace std;
char code;
int age;
double breakups;
ifstream minesRomance("filename");
if (!minesRomance) {
cerr << "Error opening input file. Heartbreaker!";
exit(1);
}
while (minesRomance >> code >> age >> breakups) {
// do stuff with each code, age and breakups value
}
minesRomance.close();
Notice how the datatypes of variables code
, age
and breakups
correspond to the data values you expect in the file. In other words, you wouldn't do this:
char age;
int breakups;
// ...
while (minesRomance >> code >> age >> breakups) {
// do stuff with each code, age and breakups value
}
Do you see why? The >> operator is sensitive to the datatypes of destination variables. Each time through this loop, a char
, another char
and an int
will attempt to be extracted from the file. The first time through the loop, variables code
, age
and breakups
would have the values 'O'
, '8'
and 76
. That's not what you want. Here's another bad example:
int age;
double breakups;
// ...
while (minesRomance >> age >> breakups >> code) {
// do stuff with each code, age and breakups value
}
Notice how the variable in the while
condition are not quite in the right order, despite having the proper datatypes.
So again, pay attention to datatypes and the order of the data when extracting "rows" of data at a time:
int age;
double breakups;
// ...
while (minesRomance >> code >> age >> breakups) {
// do stuff with each code, age and breakups value
}
Program the right way, green like a cuke.
Instructions
This assignment is similar to 10_dataAverage but this time, you will extract additional data from an input file, calculate additional simple stats, and write the results to a file.
Given a text file containing the population, land area (in sq. miles), and density of America's cities, write a program that computes the average, min, max and range of each of the three city attributes. Lastly, write the results to a text file called population_stats.txt
, so you can email the results to a prospective date you are trying to impress.
Your project includes a file called populations.txt
. It contains rows of three numbers: population, land area, and population density. It looks like this:
3792621 468.7 8091.8
...
Your program should iterate over the rows of data, extract three numbers at a time and determine the average, min, max and range of each of the three attributes (population, land area, density). Once you have these simple stats computed, you should write the results to a text file called population_stats.txt
that looks like this:
Statistic Ave Min Max Range
Population 306696 100097 8175133 8075036
Land Area 99.5851 6.4 1704.7 1698.3
Density 4151.06 171.2 27016.3 26845.1
Awesome!
Note that this general idea is applicable in many engineering contexts where a large amount of data needs to be processed and summarized. It is also a very impressive way of getting a first date.
Hints
Notice the nice aligned formatting of the table. You do not need to use the formatting features of streams: luckily, you can just use a tab character, '\t'
. We're not that concerned about the alignment of the data for this assignment.
Requirements and Rubric
A friendly message from The Terminator, our grading program
*bzzzt* Hel-lo. I will check for these specific lines in a file called "population_stats.txt":
- "Population statistics of America's N cities." where N is an integer representing the number of rows in the input file.
- "Statistic Ave Min Max Range"
- "Population A B C D" where A, B, C and D are correct numeric values for ave, min, max and range of population.
- "Land Area A B C D" where A, B, C and D are correct numeric values for ave, min, max and range of land area.
- "Density A B C D" where A, B, C and D are correct numeric values for ave, min, max and range of density.
Your program must not require any user input (no use of cin
).
Your program must terminate after writing the above output to a file.
This work is worth 80 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 2 | |
Correct datatypes used | 5 | |
Opens input file for reading | 5 | |
Checks input file for error | 5 | |
Properly closes input file | 5 | |
Opens output file for writing | 5 | |
Checks output file for error | 5 | |
Properly closes output file | 5 | |
Successfully writes to the file population_stats.txt |
5 | |
Correctly writes total number of cities to file | 2 | |
Correctly writes all twelve values in the table to file | 36 |
Concepts Exercised: declaring facts, operations & expressions, loops, making decisions, I/O
© 2011 Yong Joseph Bakos.