{ wordLengths.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. Limitations: The input is assumed to be correctly formatted. In particular, blank lines (word length 0) will crash the program, and extraneous white space will cause mistakes in the data collection. } 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} i, length, numberOfXs: integer; c : char; begin {main program} {Initialize the frequency array. No words have been read, so no word lengths have been recorded.} for length := 1 to MaxWordLength do frequency[length] := 0; {For each word, compute and record length.} while not eof do begin length := 0; while not eoln do begin read( c ); length := length + 1 end; readln; frequency[length] := frequency[length] + 1 end; {Report results in numeric and histogram form.} for length := 1 to MaxWordLength do begin write( length:2, frequency[length]:6, ' ' ); numberOfXs := frequency[length] div OccurrencesPerX; for i := 1 to numberOfXs do write( 'x' ); writeln end end.