user2877117
user2877117

Reputation:

How to sort a table in COBOL?

I am learning COBOL, and I am having difficulty trying to figure out how to sort this table. I don't think I am even implementing this table correctly, so any help on how to improve this code would be great. I am lost and confused.

IDENTIFICATION DIVISION.
PROGRAM-ID. STUDENT.

DATA DIVISION. 
WORKING-STORAGE SECTION.
01 CLASSROOM-TABLE.
    05 STUDENT OCCURS 5 TIMES. 
        10 STUDENT1.
            15 STUDENT1-N PIC A(25).
            15 STUDENT1-A PIC 99. 
        10 STUDENT2.
            15 STUDENT2-N PIC A(25).
            15 STUDENT2-A PIC 99. 
        10 STUDENT3.
            15 STUDENT3-N PIC A(25).
            15 STUDENT3-A PIC 99. 
        10 STUDENT4.
            15 STUDENT4-N PIC A(25).
            15 STUDENT4-A PIC 99. 
        10 TEMP-STUDENT.
            15 STUDENT-N PIC A(25).
FIND.

END-METHOD.
            15 STUDENT-A PIC 99. 
01 I PIC 9 VALUE 0. 
01 J PIC 9 VALUE 1.

PROCEDURE DIVISION. 
MAIN-PARA.
    MOVE "MICHAELA" TO STUDENT (1) (1:25).
    MOVE 21 TO STUDENT (1) (26:2).
    MOVE "KEVIN" TO STUDENT (2) (1:25).
    MOVE 25 TO STUDENT (2) (26:2).
    MOVE "KENNY" TO STUDENT (3) (1:25).
    MOVE 16 TO STUDENT (3) (26:2). 
    MOVE "ANDREA" TO STUDENT (4) (1:25).
    MOVE 18 TO STUDENT (4) (26:2). 

    PERFORM PUT-ORDER.
    PERFORM PRINT.
STOP RUN. 

PUT-ORDER.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 4
    ADD 1 TO J  
    IF STUDENT(I)(26:2) > STUDENT(J)(26:2)
        ADD 1 TO I
        DISPLAY "INSIDE SORT"
        MOVE STUDENT(I)(26:2) TO STUDENT(5)(26:2)
        MOVE STUDENT(J)(26:2) TO STUDENT(I)(26:2)
        MOVE STUDENT(5)(26:2) TO STUDENT(J)(26:2)
    END-IF
END-PERFORM.

DISPLAY "SORT IS DONE".

END-METHOD.

PRINT.
    DISPLAY STUDENT (1).
    DISPLAY STUDENT (2).
    DISPLAY STUDENT (3).
    DISPLAY STUDENT (4).
    END-METHOD.

I know that the code is getting to the sort, since it prints both inside sort and sort is done. But when it prints the table again, everything is the same. I don't think I'm iterating through the table correctly, but all other ways I've tried gives me errors.

Upvotes: 2

Views: 10676

Answers (2)

Bruce Martin
Bruce Martin

Reputation: 10543

This both an answer and a guide to writing Cobol

01 CLASSROOM-TABLE.
   05 STUDENT OCCURS 5 TIMES. 
      10 STUDENT-Name         PIC A(25).
      10 STUDENT-Age          PIC 99. 

and the procedure becomes

MOVE "MICHAELA" TO STUDENT-Name (1).
MOVE 21         TO STUDENT-Age  (1).
MOVE "KEVIN"    TO STUDENT-Name (2).
MOVE 25         TO STUDENT-Age  (2).
  ...

You could also do

01 CLASSROOM-TABLE.
   05 Student-Table.
      10 STUDENT OCCURS 5 TIMES. 
         15 STUDENT-Name      PIC A(25).
         15 STUDENT-Age       PIC 99. 

  05 redefines Student-Table.
     10 filler                Pic X(25)  value 'MICHAELA'
     10 filler                pic 99     value 21. 
     10 filler                Pic X(25)  value 'KEVIN'
     10 filler                pic 99     value 25. 
       ....

You will also need to define a Temporary Student

  05 Temp-Student            Pic x(27).

Cobol has a Sort Verb

e.g.

 SORT WORK-FILE
  ON ASCENDING KEY SALEPERSON-SRT
  ON ASCENDING KEY INVOICE-SORT
  INPUT PROCEDURE IS 1000-PRE-SORT
  OUTPUT PROCEDURE IS 2000-POST-SORT

If you are going to do a sort in code, the easiest sort procedure to implement is the Bubble Sort. You can google it

but basically as rcgldr said, you need 2 loops

perform varying i from 1 by 1 until i > 4
   Add 1           to i giving j
   perform  until j > 5
       if Student-Age(i) > Student-Age(j)
          Move Student(i)     to Temp-Student
          Move Student(j)     to Student(i)
          Move Temp-Student   to Student(j)
       end-if
       add 1                  to j
   end-perform
end-perform 

Upvotes: 4

rcgldr
rcgldr

Reputation: 28828

You need an inner loop for J, and I only goes to 3.

PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
    PERFORM VARYING J FROM I+1 BY 1 UNTIL J > 4
        IF ...
        END-IF
    END-PERFORM
END-PEFORM

Remove the ADD 1 TO I.

I don't know if other changes are needed.

Upvotes: 0

Related Questions