Program 4: The Sports Booster Club

The Task

The problem is to tabulate and summarize the donations to a sports booster club. An example input follows; each row gives the name of the donor, a number which is the month of the year in which the donation was made, and the amount, in dollars.

Fred 3 50.00
Jim 4 25.00
June 5 25.00
Fred 5 10.00
Emily 6 15.00
Antonio 6 10.00
Patrick 2 5.00
Patrick 3 5.00
Patrick 4 5.00

etc.

Note that the donations can appear in any order in the file, and one person can donate several times during the year.

Your task is to read all the input an store it in suitably declared arrays. Then display the data in table form on the screen (here you may save the display table in a file to be printed on the printer). This will look something like:

Name     Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
Fred     0.00  0.00  50.00 0.00  10.00 0.00  0.00  0.00  0.00  0.00  0.00  0.00
Jim      0.00  0.00  0.00  10.00 0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
June     0.00  0.00  0.00  0.00  25.00 0.00  0.00  0.00  0.00  0.00  0.00  0.00
Emily    0.00  0.00  0.00  0.00  0.00  15.00 0.00  0.00  0.00  0.00  0.00  0.00
Antonio  0.00  0.00  0.00  0.00  0.00  10.00 0.00  0.00  0.00  0.00  0.00  0.00
Patrick  0.00  5.00  5.00  5.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

etc.

Then find each person's total donation, and display the names and corresponding totals in descending order of total donation, i.e. biggest total first. The output woud then look like:

Name     Total  
Fred     60.00  
June     25.00  
Patrick  15.00 
Emily    15.00
Antonio  10.00
Jim      10.00

Procedure

Follow the same procedure for this program that you have for the previous ones, as follows:

Note that steps 1 to 6 are best done away from the computer.

  1. Read the description of exercise 17 on page 258 and write a one sentence problem statement.
  2. Write a short paragraph as a problem description that contains only relevant information. This will come from the description in the book, but can be reworked to leave out parts that will not end up in your program.
  3. Analyze the description and draw a top-down stepwise refinement diagram that has at least three levels in it. Choose brief descriptions for each of the boxes in the diagram that can end up as names you use in your program source code. Don't forget to make at least two boxes as the breakdown of each box which you refine.
  4. Take the structure of the diagram and translate it directly into the structure of your Pascal program, i.e. write a skelton program with procedure declarations only. You must use as many procedures as there are boxes in the diagram, except for the top-most box which corresponds to the program itself, or the lowest level boxes which may translate directly into Pascal statements.
  5. Choose appropriate variables that can be declared as global and passed selectively to the procedures as needed. Choose the type of the variables carefully, and only use VAR parameters where you intend a procedure to pass a value back out.
  6. Complete the source code with correct Pascal syntax, and appropriate comments that come straight from your top-down analysis.
  7. Type the source code into the Turbo Pascal editor, compile the program and run it with the data listed in the box to the left of exercise 7.
  8. You may need to change the program to get it to compile and run successfully. This is normal. However, if your changes turn out to be major you should revise your design document so that the design and the program code are in sync at all times.
  9. Print the source code and the output screen form running your program, and turn in these two printouts, and your design document on Wednesday, November 20th., by 5pm.

Hints

  1. The key is what kind of arrays to declare to store the data read from the file. I suggest you have three parallel arrays: one for the names, one (a two-dimensioned array) for the monthly donations, and one for the totals. These will look like the following:
  2. Use the following selection sort procedure, modified from that on page 323 ( you will need to change the type names, at least):

PROCEDURE Sort(VAR Names:nameArrayType; VAR Totals:totalArrayType;
                 Donations: integer);
VAR
  IndexOfLargest, StartIndex, Index : integer;
  TempTotal : real;
  TempName : string[20];
BEGIN
  FOR StartIndex := 1 TO Donations DO
    BEGIN
       IndexOfLargest := StartIndex;
       FOR Index := StartIndex + 1 TO Donations DO
         IF Totals[Index] > Totals[IndexOfLargest] THEN
            IndexOfLargest := Index;
       IF IndexOfLargest <> StartIndex THEN { do the swap }
          BEGIN
             TempTotal := Totals[IndexOfLargest];
             TempName := Names[IndexOfLargest];
             Totals[IndexOfLargest] := Totals[StartIndex];
             Names[IndexOfLargest] := Names[StartIndex];
             Totals[StartIndex] := TempTotal;
             Names[StartIndex] := TempName
          END { of swap }
     END { of outer FOR loop }
END; { of procedure Sort }

Due date

Hand in you design document, source code and sample output by Wednesday, 21st. November, before 5pm.