Accumulators

A person preparing a report manually would probably obtain the summary data by first typing all the detail lines and then going back and adding up all of the typed figures to get the necessary column totals. This would be impractical for the computer to do. Instead, a program must accumulate its summary totals from each detail record as that record is processed. After the last record is processed, the summary total calculations will be complete. Then final output of the summary line requires only that the summary totals be edited, and printed. All of this can be accomplished by defining a data field in the WORKING-STORAGE SECTION for each summary total required and adding the values for each detail record to these data fields as they are calculated. Data fields used in this way are called accumulators because they are used to accumulate totals.

 

To use a data field as an accumulator, you must:

  1. Define the accumulator. Do this in the WORKING-STORAGE SECTION. The elementary item must have a numeric PICTURE, and it must be large enough to contain the largest value that might be accumulated.

 

01  ACCUMULATORS.

02        TOTAL-BALANCE  PIC 9(7)V99  VALUE ZEROS.

 

  1. Initialize the accumulator. To accumulate a true total, the accumulator must start with an initial value of zero. This is usually accomplished by including the VALUE ZERO clause in the accumulator definition. However, it is also possible, and sometimes necessary, to initialize the accumulator by moving zeros to it in the PROCEDURE DIVISION.

 

MOVE ZEROS TO TOTAL-BALANCE

or

                        INITIALZE ACCUMULATORS

 

  1. Add to the accumulator. Add the necessary value to the value already in the accumulators, as each record is processed.

 

ADD CURR-BALANCE TO TOTAL-BALANCE

 

  1. Edit and print the accumulator’s value. After detail processing is complete, move each accumulator’s value to an edited output field in a summary print line and then write from that record to the printer.

 

MOVE TOTAL-BALANCE TO SUMRY-LINE-BALANCE

WRITE PRINT-LINE FROM SUMRY-LINE

                        AFTER ADVANCING 2 LINES