|
|
|
|
|
|
|
for (index = 0; index < length; index++)
// Invariant (prior to test):
// charList[0..index-1] and freqList[0..index-1]
// have been printed
// && 0 <= index <= length
cout << charList[index] << " occurred "
<< setw(3) << freqList[index] << " time(s)" << endl;
} |
|
|
|
|
|
|
|
|
Let's do a partial code walk-through of this program with the following data. The characters to be counted are |
|
|
|
 |
|
|
|
|
a e i o u |
|
|
|
|
|
|
|
|
and the text is
Roses are red,
violets are blue.
If I can learn C++, |
|
|
|
|
|
|
|
|
Here are the contents of the arrays after functions GetCharList and ZeroFreqList are executed: |
|
|
|
|
|
|
|
|
charList[0] is 'a' freqList[0] is 0 length is 5
charList[1] is 'e' freqList[1] is 0
charList[2] is 'i' freqList[2] is 0
charList[3] is 'o' freqList[3] is 0
charList[4] is 'u' freqList[4] is 0 |
|
|
|
|
|
|
|
|
We assume that the control structure of the reading loop in the main function is correct and look at the three inner statements: |
|
|
|
|
|
|
|
|
1. ScanList(charList, length, inputChar, index, found);
2. if (found)
freqList[index]++;
3. dataFile.get(inputChar); |
|
|
|
|
|
|
|
|
The following table shows the partial walk-through. The number to the left of the period refers to one of the three statements listed above, and the number to the right of the period refers to the number of times the statement has been executed. |
|
|
|
|
|