Homework 11: Data Average
This assignment is designed to exercise your ability to read data from a file.
Concepts
Focus on one main concept for this assignment: how to read data from an "input file stream" or ifstream
object.
Working with Data
You should have learned in class about how data is often treated as "streams" of information that can be read a piece at a time. The files we will start with are simple text files that, in this case, contain a series of numbers. Remember that whenever you work with a file stream as input, we call them ifstream
objects.
There will always be four things you will do whenever working with an ifstream
. Open the file, check for an error, read its data, and close the file. The typical pattern for this is as follows:
#include <fstream>
using namespace std;
int age;
ifstream myCatsAges("filename"); // open the file
// check for an error
if (!myCatsAges) {
cerr << "Error opening input file";
exit(1);
}
// read the data and do something with it
while (myCatsAges >> age) {
cout << "One cat is " << age << " years old.\n";
}
myCatsAges.close(); // close the file
Remember, once you have an ifstream
object (like myCatsAges
shown above) you use it in a manner similar to using cin
.
Instructions
For this assignment, you are provided a text file that contains the populations of America's cities. Your goal is to create a program that can read data in the file, and print the average population. The contents of the file look like this (but with many more numbers):
8675309
etc...
The interaction for this program is simple:
If your program generates the above output, then your program is likely correct.
Your program must read the data in the provided file using the typical "data reading" boilerplate: open the file, check for an error, use a while
loop to read sucessive data from the stream, and close the file.
Hints
Your program should not explicitly divide by the number 275 when computing the average. It should count the number of data entries in the file. In other words, your program should correctly compute the average when given a data file of any number of records.
Your nose should tell you that the filename to be used should be a constant. You might try something like this:
ifstream populations(FILENAME);
But, you'll find that doesn't quite work. It's because of the way ifstream
s expect to receive a filename that is in a "particular format" we'll simply call "c-strings." What this means is, you'll need to convert that string
to a "c-string." But, to do so is easy and looks like this:
ifstream populations(FILENAME.c_str());
Requirements and Rubric
A friendly message from The Terminator, our grading program
*bzzzt* Hel-lo. I will check for the specific output:
- "The average population of America's N cities is A." where N is the number of records in the data file and A is the average of those numbers.
Your program must not require any user input (no use of cin
).
Your program must terminate after printing the above output.
This work is worth 38 points.
Requirement | Points | Notes |
---|---|---|
Correct submission of src directory as a .zip file. | 2 | |
Place your name in the comment header in main.cpp | 2 | |
Constant used for filename | 2 | |
Proper opening of input file | 4 | |
Proper error checking of input file | 4 | |
Proper closing of input file | 4 | |
Correctly prints the number of cities | 10 | |
Correctly prints the average population | 10 |
Concepts Exercised: I/O, repeating tasks, making decisions, declaring facts, using libraries
© 2011 Yong Joseph Bakos.