cobolgnucobol

How to convert c++ code to cobol correctly


I have written code to get the number that appears the most in an array. The main function will make a call to readData which will read data into the array that I passed to the readData subroutine. After the readData subroutine has executed, i use the mode function to find the number that appears most in an array.

Example array: |1|2|2|1|1|

Output generated: 1

My c++ code is as follows:

#include <iostream>

using namespace std;

void mode(int array[]){
    int max = 0, num;
     int count;
    
    for(int i=0; i<5; ++i){
        count = 0;
        for(int j=0; j<5; ++j){
            if(array[i] == array[j]){
                ++count;
            }
             if(max < count){
                max = count;
                num = array[i];
            }
        }
    }
    
    cout << num << endl;
}

void arrayData(int array[]){
    for(int i=0; i<5; i++){
        cin >> array[i];
    }
    
    mode(array);
}

int main()
{
    int array[5];
    arrayData(array);

    return 0;
}

I would like to convert the above c++ code to cobol, I am using gnuCobol. The code i have generated so far is bellow.

IDENTIFICATION DIVISION.
PROGRAM-ID. Main.

DATA DIVISION.
    
WORKING-STORAGE SECTION.
    01 array.
        05 val PIC X(1) OCCURS 5 TIMES.

PROCEDURE DIVISION.
    CALL 'READDATA' USING array.
STOP RUN.

*> readData: fills the array with user diffined digits
IDENTIFICATION DIVISION.
PROGRAM-ID. READDATA.

WORKING-STORAGE SECTION. ***> error appears here**
    01 array.
        05 val PIC X(1) OCCURS 5 TIMES.

PROCEDURE DIVISION.
    PERFORM VARYING I FROM 1 BY 1
        UNTIL I > 5
        SET I TO 1
        ACCEPT array(I) FROM SYSIN.
        SET I UP BY 1
    END-PERFORM.
    
    CALL 'MODE' USING array.
    
EXIT PROGRAM.

*> mode: gets the number that appears most in the array
IDENTIFICATION DIVISION.
PROGRAM-ID. MODE.

WORKING-STORAGE SECTION.
    01 array.
        05 val PIC X(1) OCCURS 5 TIMES.
    01 maxv PIC(1) VALUE 0.
    01 counter PIC(1).
    01 num PIC(1).

PROCEDURE DIVISION.
    PERFORM VARYING I FROM 1 BY 1
        UNTIL I > 5
            MOVE 0 TO counter
        
            PERFORM VARYING J FROM 1 BY 1
            UNTIL J > 5
            
            IF array(I) = array(I) THEN
                SET counter UP BY 1
            END-IF
            
            IF maxv < counter THEN
                MOVE counter TO maxv
                MOVE array(I) TO num
            END-IF
            
            SET J UP BY 1
            END-PERFORM.
            
        SET I UP BY 1
    END-PERFORM.
    
    DISPLAY "Mode: "num.
EXIT PROGRAM.
 

I am getting the following error:

Error: syntax error, unexpected "WORKING-STORAGE", expecting "END PROGRAM" or "PROGRAM-ID"


Solution

  • There were several issues with the code. The reported error appears to have been the absence of an END PROGRAM statement to separate the first program from the second.

    Other errors include:

    • Using SET statements unnecessarily

    • Passing the array without a LINKAGE SECTION or USING phrase

    • Missing DATA DIVISION statements

    • Missing data items

    • Improper subscripting

    • Including a "separator period" before a scope terminator

    • Invalid PICTURE clauses

    These issues are shown in comments in the following code. Compare the original code with the modified code.


    Modified code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. Main.
    
       DATA DIVISION.
            
       WORKING-STORAGE SECTION.
       01 array.
           05 val PIC X(1) OCCURS 5 TIMES.
    
       PROCEDURE DIVISION.
           CALL 'READDATA' USING array.
       STOP RUN.
       END PROGRAM MAIN. *> ADDED
    
       *> readData: fills the array with user diffined digits
       IDENTIFICATION DIVISION.
       PROGRAM-ID. READDATA.
       DATA DIVISION. *> ADDED
       WORKING-STORAGE SECTION. *> error appears here**
       01 I PIC 9.
       LINKAGE SECTION. *> ADDED
           01 array.
               05 val PIC X(1) OCCURS 5 TIMES.
    
       PROCEDURE DIVISION USING ARRAY. *> MODIFIED
           PERFORM VARYING I FROM 1 BY 1
               UNTIL I > 5
       *>      SET I TO 1 *> REMOVED
               ACCEPT VAL   (I) FROM SYSIN  *> MODIFIED
       *>      SET I UP BY 1 *> REMOVED
           END-PERFORM.
            
           CALL 'MO-DE' USING array.
            
           EXIT PROGRAM.
       END PROGRAM READDATA. *> ADDED
    
       *> mode: gets the number that appears most in the array
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MO-DE. *> 'MODE' IS A RESERVED WORD
       DATA DIVISION. *> ADDED
       WORKING-STORAGE SECTION.
       01 maxv PIC 9 VALUE 0. *> MODIFIED
       01 counter PIC 9. *> MODIFIED
       01 num PIC 9. *> MODIFIED
       01 I PIC 9. *> ADDED
       01 J PIC 9. *> ADDED
       LINKAGE SECTION. *> ADDED
       01 array.
           05 val PIC X(1) OCCURS 5 TIMES.
    
       PROCEDURE DIVISION USING ARRAY. *> MODIFIED
           PERFORM VARYING I FROM 1 BY 1
               UNTIL I > 5
                   MOVE 0 TO counter
                
                   PERFORM VARYING J FROM 1 BY 1
                   UNTIL J > 5
                    
                   IF VAL  (I) = VAL  (J) THEN *> MODIFIED
                       ADD 1 TO COUNTER *> MODIFIED
                   END-IF
                    
                   IF maxv < counter THEN
                       MOVE counter TO maxv
                       MOVE VAL  (I) TO num
                   END-IF
                    
       *>          SET J UP BY 1 *> REMOVED
                   END-PERFORM *> MODIFIED
                    
       *>      SET I UP BY 1 *> REMOVED
           END-PERFORM.
            
           DISPLAY "Mode: "num.
           EXIT PROGRAM.
       END PROGRAM MO-DE. *> ADDED
    

    Input:

    1
    5
    5
    2
    3
    

    Output:

    Mode: 5