< previous page page_621 next page >

Page 621
Because you don't know how many characters are in the list, charList has to be large enough to hold all the characters in the character set except one. ('#' is a sentinel value and is not counted.) When charList has been read, counter holds the length of the array; that is, when the list is searched for a character, only those components from charList[0] through charList[length-1] need be examined. This is another example of subarray processing. Note that counter is being used both as a counter and as an index.
Zero FreqList (parameters discussed later)
FOR index going from 0 through length-1
  Set freqList[index] = 0

Scan List (parameters discussed later)
Set index = 0
WHILE index < length AND inputChar != charList[index]
   Increment index
Set found = (index < length)

The algorithm for the Scan List module is called a sequential search. We start with the first array element and step through the array one element at a time. We continue searching until we either find the item we're looking for or reach the end of the array. If found is TRUE, the value of index is the position in array charList where inputChar was found. If inputChar is not in the array, then index becomes equal to length, and found is FALSE.
The While condition assumes the use of short-circuit evaluation of logical expressions (Chapter 5). If index equals length, the condition is immediately determined to be FALSE. The second half of the condition is not evaluated (which would reference a nonexistent array element, charList[length]).
Increment FreqList
Increment freqList[index] by 1

 
< previous page page_621 next page >