{ wordsWithProcs.p Started by Jeff Ondich on 1/19/96 Last modified on 1/23/96 This program reads a list of words (one word per line of text) from standard input, and records the frequency with which each word length occurs. At end-of-file, the the program reports the results for word lengths 1 to MaxWordLength, using both exact numerical and histogram display. *** I wrote this one using procedures. I'm not completely convinced that this version of the program is more readable than the original, but maybe it is. What do you think? *** } program wordLengths(input,output); const MaxWordLength = 30; OccurrencesPerX = 1000; type IntArray = array[1..MaxWordLength] of integer; var frequency : IntArray; {Keeps track of the number of times each word length occurs} procedure Initialize( var freq : IntArray ); { Is this 2-line piece of code really worth a procedure? } var length : integer; begin for length := 1 to MaxWordLength do freq[length] := 0 end; procedure ProcessWords( var freq : IntArray ); { For each word, compute the word's length and add one to the corresponding frequency counter. } var length : integer; c : char; begin while not eof do begin length := 0; while not eoln do begin read( c ); length := length + 1 end; readln; freq[length] := freq[length] + 1 end end; procedure ReportResults( freq : IntArray ); { Report the frequency with which each word length occurred using both numeric and histogram form. } var i, length, numberOfXs : integer; begin for length := 1 to MaxWordLength do begin write( length:2, freq[length]:6, ' ' ); numberOfXs := freq[length] div OccurrencesPerX; for i := 1 to numberOfXs do write( 'x' ); writeln end end; begin {main program} Initialize( frequency ); ProcessWords( frequency ); ReportResults( frequency ); end.