GreenSaucer42
GreenSaucer42

Reputation: 49

COBOL programmers - How to use arrays

I am programming in COBOL and trying to put this client file in an array. I'm having trouble understanding this problem. I know that the array would probably be based on the bookingtype because there are 4 different options. Any help would be appreciated.

This is how I have the array defined so far:

01 Booking-Table.                                    
   05 BookingType          OCCURS 4 TIMES     PIC 9. 

Here is the client file.

Here is the problem

Upvotes: 0

Views: 886

Answers (2)

Edward H
Edward H

Reputation: 586

I believe the diagram is trying to say you need an enumeration. In COBOL, you'd implement this with

01  client-file-record.
    *> ...
    03  booking-type         PIC 9.
        88 cruise            VALUE 1.
        88 air-independent   VALUE 2.
        88 air-tour          VALUE 3.
        88 other             VALUE 4.
    *> ...

An array-approach is only necessary if the booking types (and/or their behaviour) varied at runtime.

Upvotes: 0

Simon Sobisch
Simon Sobisch

Reputation: 7288

I guess the solution is about storing the costs in an array. To calculate the average the array would need to have cost + number with the booking type being the index used. The "tricky" part may be the maximum of amount per type (9999.99) * maximum customers with this type (all and as the client number implies the 3 given positions are numeric: 1000 [including the zero, all could have the same type]).

Something like

REPLACE ==MaxBookingType== BY ==4==.

01 Totals-Table.
   05 Type-Total  OCCURS MaxBookingType TIMES.
      10 type-amount      pic 9(8)V99 COMP.
      10 type-customers   pic 9(4)    COMP.

Now loop through the file from start to end, do check that BookingType >= 1 AND <= MaxBookingType (I'm always skeptic that "data never changes and is always correct) and then

ADD 1         TO type-customers(BookingType)
ADD trip-cost TO type-amount   (BookingType)

and after end of file calculate the average for all 4 entries using a PERFORM VARYING.

The main benefit of using an "array" here is that you can update the program to have 20 booking types just by changing the value for MaxBookingType - and as you've added a check which tells you what "bad" number is seen in there you can adjust it quite fast.

I'm not sure if/how your compiler does allow self-defined numeric constants, if there's a way: use this instead of forcing the compiler to check for all occurrences of the text "MaxBookingType".

Upvotes: 2

Related Questions