title: Using arrays in cobol to loop through clients and booking type
body: <p><strong>Trying to solve this using arrays.</strong> </p>
  
  <p>Here's the problem: <a href="https://i.stack.imgur.com/FqTHx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FqTHx.jpg" alt="enter image description here"></a></p>
  
  <p><em>Problem I'm facing:</em> Has to be an easier way to loop through the booking types. When I view the output, it shows the customer number, customer name, and address several times with the same input which it shouldn't be. </p>
  
  <p>Any help would be appreciated.</p>
  
  <p><strong>Here is the program below:</strong></p>
  
  <pre><code> Process Apost.
   Identification Division.
   Program-ID. BOOKINGARR.
  
  *
  * Page 554 No 3. ARRAYS.
  * Data in sequence by Client No. Print the average cost of 
  * trip for each booking type. Use arrays.
  * 
   Environment Division.
   Configuration Section.
   Source-Computer.  IBM-AS400.
   Object-Computer.  IBM-AS400.
   Input-Output Section.
   File-Control.
       Select Input-File Assign to Database-Bookingpf.
       Select Output-File Assign to Printer-Qsysprt.
   Data Division.
   File Section.
  
    FD Input-File.
    01 Input-File-Rec.
        Copy DDS-BookingR of Bookingpf.
  
    FD Output-File.
     01 Output-File-Rec         Pic x(120).
    Working-Storage Section.
  
      01 END-OF-FILE          PIC X           VALUE 'N'.
  
      01 WS-ARRAY.
         05 WS-TABLE-ENTRIES OCCURS 4 TIMES.
            10 WS-TOTAL-COST                 PIC 9(7)V99.
            10 WS-TRIP-COUNT                 PIC 999.
            10 WS-AVG-COST                   PIC 9(7)V99 VALUE ZERO.
            10 WS-BOOKING-TYPE               PIC 9.
  
       01 ARRAY-INDEX                         PIC 99.
       01 EMPTY-POINTER                       PIC 99.
       01 ARRAY-EMPTY                         PIC XXX.
  
  
        01 PROGRAM-HEADER.
           05                        PIC X(2)  VALUE SPACES.
           05                        PIC X(10) VALUE 'CLIENT NO.'.
           05                        PIC X(3)  VALUE SPACES.
           05                        PIC X(11) VALUE 'CLIENT NAME'.
           05                        PIC X(6)  VALUE SPACES.
           05                        PIC X(14) VALUE 'CLIENT ADDRESS'.
           05                        PIC X(4)  VALUE SPACES.
           05                        PIC X(9)  VALUE 'BOOK TYPE'.
           05                        PIC X(4)  VALUE SPACES.
           05                        PIC X(12) VALUE 'AVERAGE COST'.
        01 REPORT-LINE.
           05                        PIC X(2)  VALUE SPACES.
           05  CLIENTNO-OUT          PIC 999.
           05                        PIC X(10) VALUE SPACES.
           05  CLIENTNA-OUT          PIC X(16).
           05                        PIC X(1)  VALUE SPACES.
           05  CLIENTADD-OUT         PIC X(19).
           05                        PIC X(3)  VALUE SPACES.
           05  BOOKTYPE-OUT          PIC Z.
           05                        PIC X(8) VALUE SPACES.
           05  AVGCOST-OUT           PIC $Z,ZZ9.99.
           05                        PIC X(12) VALUE SPACES.
  
      Procedure Division. 
      000-MAIN.
        OPEN INPUT INPUT-FILE 
            OUTPUT OUTPUT-FILE.
        PERFORM 100-MOVE.
        PERFORM 1000-READ.
        PERFORM 300-UPDATE-BOOKINGS
                UNTIL END-OF-FILE = 'Y'.
        WRITE OUTPUT-FILE-REC FROM PROGRAM-HEADER.
        PERFORM 600-WRITE-TO-SCREEN
                VARYING ARRAY-INDEX FROM 1 BY 1
                UNTIL ARRAY-INDEX &gt; 4.
        CLOSE INPUT-FILE, OUTPUT-FILE.
        STOP RUN.
  
      100-MOVE.
         MOVE 1      TO EMPTY-POINTER.
         MOVE 'Y'    TO ARRAY-EMPTY.
         MOVE SPACES TO UPDATE-DONE.
         PERFORM 150-ZERO-OUT-ARRAY
                 VARYING ARRAY-INDEX FROM 1 BY 1 UNTIL
                 ARRAY-INDEX &gt; 4.
  
      150-ZERO-OUT-ARRAY.
         MOVE ZEROS TO  WS-BOOKING-TYPE    (ARRAY-INDEX).
         MOVE ZEROS TO  WS-TOTAL-COST      (ARRAY-INDEX).
         MOVE ZEROS TO  WS-TRIP-COUNT      (ARRAY-INDEX).
         MOVE ZEROS TO  WS-AVG-COST        (ARRAY-INDEX).
  
      1000-READ.
         READ INPUT-FILE AT END MOVE 'Y' TO END-OF-FILE.
  
  
      300-UPDATE-BOOKINGS.
       IF ARRAY-EMPTY = 'Y'
          PERFORM 400-ADD-1-TO-COUNT
          MOVE 'N' TO ARRAY-EMPTY
       ELSE
          MOVE 'N' TO UPDATE-DONE
          PERFORM 500-GET-BOOKING-AVERAGE
             VARYING ARRAY-INDEX FROM 1 BY 1
             UNTIL ARRAY-INDEX = EMPTY-POINTER
                        OR
             UPDATE-DONE = 'Y'.
  
       IF UPDATE-DONE = 'N'
          PERFORM 400-ADD-1-TO-COUNT.
  
       PERFORM 1000-READ.
  
     400-ADD-1-TO-COUNT.
       MOVE BOOKTYPE   TO WS-BOOKING-TYPE (EMPTY-POINTER).
       ADD 1           TO WS-TRIP-COUNT   (EMPTY-POINTER).
       MOVE COSTOFTRIP TO WS-TOTAL-COST   (EMPTY-POINTER).
       MOVE COSTOFTRIP TO WS-AVG-COST     (EMPTY-POINTER).
       ADD 1 TO EMPTY-POINTER.
  
     500-GET-BOOKING-AVERAGE.
      IF BOOKTYPE = WS-BOOKING-TYPE      (ARRAY-INDEX)
         ADD 1 TO WS-TRIP-COUNT          (ARRAY-INDEX)
         ADD COSTOFTRIP TO WS-TOTAL-COST (ARRAY-INDEX)
         COMPUTE WS-AVG-COST (ARRAY-INDEX) =
              WS-TOTAL-COST (ARRAY-INDEX) / 
              WS-TRIP-COUNT (ARRAY-INDEX) 
         MOVE 'Y' TO UPDATE-DONE.
  
     600-WRITE-TO-SCREEN.
        MOVE CLIENTNO TO CLIENTNO-OUT.
        MOVE CLIENTNA TO CLIENTNA-OUT.
        MOVE CLIENTADD TO CLIENTADD-OUT.
        MOVE WS-BOOKING-TYPE (ARRAY-INDEX) TO BOOKTYPE-OUT.
        MOVE WS-AVG-COST (ARRAY-INDEX) TO AVGCOST-OUT.
        WRITE OUTPUT-FILE-REC FROM REPORT-LINE 
              AFTER ADVANCING 1 LINE. 
  </code></pre>