IDENTIFICATION DIVISION. PROGRAM-ID. D2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO "IN/2.TXT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-LINE PIC X(25). WORKING-STORAGE SECTION. 01 EOF PIC X VALUE 'N'. 01 SAFE-COUNT PIC 9(5) VALUE 0 . 01 DIFF PIC S9(5) . 01 IDX PIC 9(5) . 01 PTR PIC 9(5) VALUE 1 . 01 NOS OCCURS 8 TIMES. 05 ELT PIC X(2) VALUE ' '. 01 OLD-SLOPE PIC X VALUE ' '. 01 SLOPE PIC X VALUE ' '. 01 SAFETY PIC X . 88 SAFE VALUE 'Y'. 88 UNSAFE VALUE 'N'. PROCEDURE DIVISION. OPEN INPUT INPUT-FILE. PERFORM PART-1 UNTIL EOF = "Y". DISPLAY SAFE-COUNT. CLOSE INPUT-FILE. STOP RUN. PART-1. READ INPUT-FILE AT END MOVE "Y" TO EOF NOT AT END PERFORM PARSE-LINE PERFORM SAFE-CHECK END-READ. PARSE-LINE. MOVE 1 TO PTR. PERFORM VARYING IDX FROM 1 BY 1 UNTIL PTR = LENGTH INPUT-LINE UNSTRING INPUT-LINE DELIMITED BY SPACES INTO ELT(IDX) WITH POINTER PTR END-PERFORM. SAFE-CHECK. MOVE ' ' TO OLD-SLOPE SLOPE. SET SAFE TO TRUE. PERFORM VARYING IDX FROM 2 BY 1 UNTIL UNSAFE OR ELT(IDX) = SPACE OR IDX > 8 PERFORM CALC-DIFF-SLOPE PERFORM SAFETY-COND MOVE SLOPE TO OLD-SLOPE END-PERFORM. DISPLAY INPUT-LINE ' ' SAFETY IF SAFE ADD 1 TO SAFE-COUNT END-IF. CALC-DIFF-SLOPE. COMPUTE DIFF = FUNCTION NUMVAL(ELT(IDX)) - FUNCTION NUMVAL(ELT(IDX - 1)). IF DIFF < 0 MOVE 'N' TO SLOPE ELSE MOVE 'P' TO SLOPE END-IF. SAFETY-COND. IF OLD-SLOPE NOT= SPACE AND OLD-SLOPE NOT= SLOPE SET UNSAFE TO TRUE END-IF. IF FUNCTION ABS(DIFF) < 1 OR FUNCTION ABS(DIFF) > 3 SET UNSAFE TO TRUE END-IF.