Help with array problem.



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Help with array problem.

Postby erich » Thu Apr 19, 2018 1:25 am

Problem I'm facing: 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.

Any help would be appreciated.


PROBLEM IN BOOK: 4 different booking types.

CLIENTNO. | CLIENTNAME | CLIENTADDRESS | BOOKINGTYPE | COSTOFTRIP

1 = cruise
2 = air-indpnt
3 = air-tour
4 = other
Data is in sequence by clientno. Print the average cost of a trip for each booking type. Use arrays.

Here is the program below:



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 > 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 > 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.
 



Here is the output:


 CLIENT NO.   CLIENT NAME      CLIENT ADDRESS    BOOK TYPE    AVERAGE COST    
 123          ERIC H               123 RED RD.          1            $5,000.00      
 123          ERIC H               123 RED RD.          2            $    0.00      
 123          ERIC H               123 RED RD.          3            $6,000.00      
 123          ERIC H               123 RED RD.          4            $5,000.00                                                
 
erich
 
Posts: 8
Joined: Mon Apr 16, 2018 2:54 am
Has thanked: 1 time
Been thanked: 0 time

Re: Help with array problem.

 

Re: Help with array problem.

Postby Robert Sample » Thu Apr 19, 2018 5:51 am

My comments:
1. You don't need to put the average trip cost in the array. Average trip cost will be a single number for each booking type calculated after you have read all the data.
2. You don't need to store booking type in the array. Booking type will be the array index (1 through 4) for the current record.
3. Why do you care if the array is empty? Properly written code won't have to deal with that.
4. Why are you printing the client data? The problem statement asks for average trip cost, NOT average trip cost by client.
5. COBOL does what you tell it to do, not what you want it to do. So if your output is not what you want, then you've given COBOL the wrong statements and / or the wrong sequence of statements.
Robert Sample
Global moderator
 
Posts: 3537
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 253 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post