< previous page page_629 next page >

Page 629
Problem-Solving Case Study Frequency of All Characters
0629-01.gif
Problem: Count the frequency of occurrence of all the characters in a sample of text.
Input: A file of text (dataFile).
Output: Each printable character in the character set, followed by the number of times it occurred.
Discussion: In the previous case study, the CharCount program implements an algorithm that not only uses parallel arrays and subarray processing, but parallels the way in which a human would do the problem. There is nothing wrong with this solution except that we are not taking advantage of all the information on hand. By changing the problem statement slightly, we can approach the solution from another angle, this time keeping in mind the features of C++.
In a sense, C++ already has a built-in list of all the characters-type char. C++ also allows us to use any integral type as an index type. So, instead of searching a list of characters and counting them in a parallel array, we let C++ do all that by using the characters themselves as the indices in the counting array. Here's how it works.
First, we define a named constant NOM_CHARS as the number of characters in the character set (128 for ASCII, 256 for EBCDIC). Focusing on ASCII, we use the following declarations:
const int NUM_CHARS = 128;   // Number of chars in ASCII char set

int freqCount[NUM_CHARS];    // Frequency counts
The indices for freqCount range from 0 through 127. This range corresponds to the internal representations of the 128 ASCII characters. (Recall from Chapter 10 that the character 'A' is stored internally as the integer 65, 'B' is stored as 66, and so forth.) If we input the character 'A' into the variable inputChar, the statement
freqCount[inputChar]++;

 
< previous page page_629 next page >