< previous page page_620 next page >

Page 620
Discussion: If you were doing this by hand, you would make a list of the characters you wanted to count. Then you would process the text by taking each character and checking to see whether it was on your list. If it was, you would make a hash mark beside it.
This algorithm can be used directly in a program. The list of characters can be read into an array of type char. To see whether or not a character is in the list, you scan the list, comparing the character with the ones in the list. To simulate making a hash mark, you use a second, parallel array that is the same size as the character list, but whose components are of type int. If you find the character in the list, you add 1 to the component with the same index in the second array. For example, if the first character in our list is an 'A', then each time you find an 'A', the first slot in the integer array is incremented.
Data Structures: A one-dimensional array of type char to hold the characters being counted (charList) and a one-dimensional array of type int to hold the corresponding frequencies (freqList).
Main Level 0
Open dataFile (and verify success)
Get charList
Zero out freqList
Read inputChar from dataFile
WHILE NOT EOF on dataFile
   Scan list for inputChar
   IF found
       Increment appropriate element of freqList
   Read inputChar from dataFile
Print charList and freqList

Get CharList (parameters discussed later) Level 1
Set counter = 0
Read a character ch
WHILE ch isn't '#'
   Set charList[counter] = ch
   Increment counter
   Read ch
Set length = counter

 
< previous page page_620 next page >