COBOL programmers - How to use arrays

a 夏天 提交于 2019-12-11 21:12:32

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/49845134/cobol-programmers-how-to-use-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!