CSCI 261 Programming Concepts (C++)

Winter/Spring 2012

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 <iostream>
#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):

12345
8675309
etc...

The interaction for this program is simple:

The average population of America's 275 cities is 306696.

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:

const string FILENAME = "populations.txt";

ifstream populations(FILENAME);

But, you'll find that doesn't quite work. It's because of the way ifstreams 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:

const string FILENAME = "populations.txt";

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:

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