cobol

Even or Odd number in COBOL language


I am trying to write a function in COBOL language which check if number is even or odd and return accordingly. But instead, it shows error. This is my code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EVEN-OR-ODD.
       DATA DIVISION.
       LINKAGE SECTION.
       01 NUM         PIC S9(08).
       01 R           PIC S9(08).
       01 Q           PIC S9(08).
       01 RESULT      PIC A(04).
       PROCEDURE DIVISION USING NUM.
         DIVIDE NUM BY 2 GIVING R REMAINDER Q.
         IF Q IS NOT EQUAL TO 0
         MOVE "ODD" TO RESULT
         ELSE
         MOVE "EVEN" TO RESULT
         STOP RUN.
       END PROGRAM EVEN-OR-ODD.

Instead of result either "EVEN" or "ODD", it raise an error:

attempt to reference unallocated memory (signal SIGSEGV)

 Last statement of EVEN-OR-ODD unknown
 Last statement of TESTS unknown

This is test I am using to check the code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TESTS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUM     PIC S9(8).
       01 RESULT  PIC A(4).
       PROCEDURE DIVISION.
           TESTSUITE 'Fixed tests'.
           TESTCASE 'Test 01'.
           MOVE 0 TO NUM
           INITIALIZE RESULT
           CALL 'EVEN-OR-ODD' USING BY CONTENT NUM BY REFERENCE RESULT
           EXPECT RESULT TO BE "Even".
           TESTCASE 'Test 02'.
           MOVE 1 TO NUM
           INITIALIZE RESULT
           CALL 'EVEN-OR-ODD' USING BY CONTENT NUM BY REFERENCE RESULT
           EXPECT RESULT TO BE "Odd".
           TESTCASE 'Test 03'.
           MOVE 7 TO NUM
           INITIALIZE RESULT
           CALL 'EVEN-OR-ODD' USING BY CONTENT NUM BY REFERENCE RESULT
           EXPECT RESULT TO BE "Odd".
           TESTCASE 'Test 04'.
           MOVE -42 TO NUM
           INITIALIZE RESULT
           CALL 'EVEN-OR-ODD' USING BY CONTENT NUM BY REFERENCE RESULT
           EXPECT RESULT TO BE "Even".
           TESTCASE 'Test 05'.
           MOVE -17 TO NUM
           INITIALIZE RESULT
           CALL 'EVEN-OR-ODD' USING BY CONTENT NUM BY REFERENCE RESULT
           EXPECT RESULT TO BE "Odd".
           END TESTS.
       END PROGRAM TESTS.

Solution

  • You have:

           DATA DIVISION.
           LINKAGE SECTION.
           01 NUM         PIC S9(08).
           01 R           PIC S9(08).
           01 Q           PIC S9(08).
           01 RESULT      PIC A(04).
           PROCEDURE DIVISION USING NUM.
    

    which means 4 data items that are not in the program's storage - and only NUM getting data from the caller.

    You get a memory error - very likely on the first use of any of those variables. Given the abort message this is GnuCOBOL, but you haven't enabled the runtime checks - recompiling with cobc --debug EVEN-OR-ODD.cob will likely lead to a more helpful diagnostic.

    To actually fix this issue, change the DATA DIVISION to also have program-local storage:

           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01 R           PIC S9(08).
           01 Q           PIC S9(08).
           01 RESULT      PIC A(04).
           LINKAGE SECTION.
           01 NUM         PIC S9(08).
           PROCEDURE DIVISION USING NUM.
    

    (and I'd always highly suggest to compile with full runtime checks)