< previous page page_613 next page >

Page 613
Indices with Semantic Content
In some problems, the index has meaning beyond simple position; that is, the index has semantic content. An example is the salesAmt array we showed earlier. This array is indexed by a value of enumeration type Drink. The index of a specific sales amount is the kind of soft drink sold; for example, salesAmt [ROOT_BEER] is the dollar sales figure for root beer.
The third of the three case studies that now follow (Frequency of All Characters) illustrates the use of indices with semantic content.
Problem-Solving Case Study Comparison of Two Lists
0613-01.gif
Problem: You are writing a program for an application that does not tolerate erroneous input data. Therefore, the data are prepared by entering them twice into one file. The file contains two lists of positive integer numbers, separated by a negative number. These two lists of numbers should be identical; if they are not, then a data entry error has occurred. For example, if the input file contains the sequence of numbers 17, 14, 8, -5, 17, 14, 8, then the two lists of three numbers are identical. However, the sequence 17, 14, 8, -5, 17, 12, 8 shows a data entry error.
You decide to write a separate program to compare the lists and print out any pairs of numbers that are not the same. The exact number of integers in each list is unknown, but each list has no more than 500.
Input: A file (dataFile) containing two lists of positive integers. The lists are separated by a negative integer and are of equal length.
Output: The statement that the lists are identical, or a list of the pairs of values that do not match.
Discussion: Because the lists are in the same file, the first list has to be read and stored until the negative number is read. Then the second list can be read and compared with the first list.
If we were checking the lists by hand, we would write the numbers from the first list on a pad of paper, one per line. The line number would correspond to the number's position in the list; that is, the first number would be on the first line, the second number on the second line, and so on. The first number in the second list would then be compared to the number on the first line, the second number to the number on the second line, and so forth.
We use an array named firstList to represent the pad of paper. Its declaration looks like this:

 
< previous page page_613 next page >