The input file will look something like this:
Bernice 10 23 443 24 7 Zebediah 4 55 32 1 Agatha 2 34 Neville 32 222 1 3 1 2 2
Note that this time, the name comes first. Also, the number of "scores" listed for each person is variable--Agatha has only two scores here, while Neville has seven. You may assume that no person has more than ten scores, and that there will be no more than 100 people in the data file.
Your task is to read this data file in, and print out two lists of names, maxima, and averages. The first list should be sorted alphabetically by name, and the second should be sorted by average score.
Thus, the output corresponding to the 4-person file example above should look something like this:
Name Max Avg -------------------------- Agatha 34 18.00 Bernice 443 101.40 Neville 222 37.57 Zebediah 55 23.00 Name Max Avg -------------------------- Bernice 443 101.40 Neville 222 37.57 Zebediah 55 23.00 Agatha 34 18.00
This program is big enough that you need to think carefully about how to organize it, what the interfaces of your procedures and functions should look like, and in what order you will implement and test the routines.
Here's a little gift:
{==================================================== ReadWord reads the next "word" from standard input (i.e. the keyboard, or the input file specified by "< file" at the Unix command line), returning it via the variable parameter "word". Here, a word consists of a block of contiguous non-space characters. That is, a word is surrounded by spaces, tabs, eoln, or eof. Note that punctuation will be part of a word, and that ReadWord will read past eoln when it is searching for the next word. We'll discuss this rather complicated code in class 11/1/96. If you can figure out a simpler way to reliably read words in Pascal (taking eof, eoln, and leading whitespace into account), I will be extremely pleased to see your method. ====================================================} procedure ReadWord( var word : string ); const tab = chr(9); {The tab character has ASCII value 9} space = chr(32); {The space character has ASCII value 32} type stateType = (beforeWord,duringWord,done); {See pages 198-200 of Abernethy & Allen to see what this is about.} var ch : char; state : stateType; begin word := ''; state := beforeWord; while state <> done do begin if eof then state := done else if eoln then begin if state = beforeWord then readln else state := done end else begin read( ch ); if (ch <> tab) and (ch <> space) then begin word := word + ch; state := duringWord end else if state = duringWord then state := done end end end;
Start early, stay in touch, and have fun.
The interested US citizen might consider getting this done early enough
to watch election returns on Tuesday night.