CO311: Intro to Modern Programming II

Dynamic Criteria

This week, we’re working towards a program that can load a set of criteria from one file (the criteria file) and use it as a basis for operation on another file (the data file). We’ll develop this finished program through a series of steps, starting with our previous work. We’ll be working within the example concept of a “census” to be tabulated.

Sample Files

crit.txt – sample criteria
census.txt – census data

The criteria file is a text file with a long name and 2-character abbreviation of the state. There is one state record per line, with each field separated by a space (state names with more than one word have the underscore character (‘_’) in place of the space).

Example criteria file:

New_Jersey NJ
New_York NY
Ohio OH

The census file is a text file with the first and last name, plus the state of residence for each census respondent. There is one record per line, and each field is separated by a space.

Example data file:

Michael Platner DC
Red Cavaney DC
John Felten MA
Kathleen McCarthy MA
Nancy Brickley MA
Arthur Brownstein NJ
Gary Hamilton NJ

Example Files

Example 1—Basic setup for reading census data into an array of appropriate class objects.

Example 2—Tallying two states in “quick and dirty” fashion. Notice how code is duplicated to add additional states.

Example 3—Streamlining by pulling the “Tally” out as a separate function

Example 4—Showing a whole bunch of states being tallied, as well as calculating “other” responses.

Example 5—Streamlining by creating a new class to hold criteria data.

Example 6—Tallying two states using the new class. Notice how code is duplicated to add additional states.

Example 7—Streamlining by using an array of states to hold the criteria

Example 8—Streamlining by pulling out the calculations and display routines as separate functions. This is easy now that both the data and criteria are in arrays.

Example 9—Streamlining by loading the criteria data from an external file.

Example 10—Slick Trick: Using the last element of the criteria array to hold “other” responses.


Multiple-file Split

After finally getting to Example 10, we’re left with one pretty large program. However, some of the helper functions we wrote to streamline things are obviously related to the criteria class, others are obviously related to the data class, and a third type seem to be dependent on both.

The typical way to clean up that big file is to move all the related functions, prototypes and class definitions into their own set of files. Here’s the way I would break up Example 10: