Author Topic: Anyone here know COBOL? - Help with COBOL  (Read 803 times)

I'm trying to do some COBOL homework, but right away I'm stuck. We are given this array, but it is inconsistent: 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1

What I originally thought was to load it in with an array occurring 20 times with elements of 99 and X, but then I realized the last 9 numbers are singular, so loading that into the array itself would break horribly. I tried to look up how to nicely break this up in COBOL, but it's impossible to search. I'd rather not edit the given table, since we are supposed to sort it as-is. Does anyone know how I could grab each number into my array in COBOL?

op is MegaScientifical

EDIT: for us non-megascientifical, can you please give us a brief overview on what cobol is?
« Last Edit: February 10, 2013, 04:49:56 PM by The Tavros Nitram »

op is MegaScientifical

You should cut that out and try to post helpful things rather than doing this.

I could figure out some extremely messy, overcomplicated code where it searches out each comma and tries to individually move things, which would probably not work in the end or one reason or another, like the number being counted as alphabetical for how I put it or something. But I'm sure I'm missing some code I never got to learn. I really don't want to go 40 minutes to the college just to get told a simple answer, then 40 minutes back.
« Last Edit: February 10, 2013, 04:55:34 PM by MegaScientifical »

I composed the code with adjustments to the array so it would work in a more standard fashion. Testing it in an online compiler instead of the official school one appeared to work:

Code: [Select]
      IDENTIFICATION DIVISION.
       PROGRAM-ID. LAB2.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       FILE SECTION.
       WORKING-STORAGE SECTION.
  01  Three-Arrays.
      05  Indiv-Array OCCURS 3 TIMES.
      10 Numb-Area OCCURS 20 TIMES.
                   15  Spec-Num       PIC 99.
                   15  FILLER          PIC X.
       01  Swap-Occur                  PIC X VALUE "Y".
       01  Sort-Max                    PIC 99.
       01  SLVal                       PIC 99.
       01  Bub-Sort-Hold               PIC 99.
       01  Bub-Swap-Count              PIC 9999.
       01  Bub-Comp-Count              PIC 9999.
       01  Comb-Sort-Hold              PIC 99.
       01  Comb-Swap-Count             PIC 9999.
       01  Comb-Comp-Count             PIC 9999.
       01  Comb-Static-Size            PIC 99.
       PROCEDURE DIVISION.
       100-Main-Routine.
           MOVE
       "20,19,18,17,16,15,14,13,12,11,10,09,08,07,06,05,04,03,02,01" TO
           Indiv-Array(1), Indiv-Array(2), Indiv-Array(3)
           DISPLAY "Current Array: " Indiv-Array(1)
           DISPLAY " "
           PERFORM 200-Top-Bub-Sort-Routine
           DISPLAY "Bubble-Sorted Array: " Indiv-Array(2)
           DISPLAY "Number of Swaps: " Bub-Swap-Count
           ". Number of Comparisons: " Bub-Comp-Count "."
           DISPLAY " "
           PERFORM 500-Top-Comb-Sort-Routine
           DISPLAY "Comb-Sorted Array: " Indiv-Array(3)
           DISPLAY "Number of Swaps: " Comb-Swap-Count
           ". Number of Comparisons: " Comb-Comp-Count "."
           STOP RUN.
       200-Top-Bub-Sort-Routine.
           MOVE "Y" TO Swap-Occur
           MOVE 19 TO Sort-Max
           MOVE 0 TO Bub-Swap-Count
           MOVE 0 TO Bub-Comp-Count
           PERFORM 300-Main-Bub-Sort-Routine UNTIL Swap-Occur = "N".
       300-Main-Bub-Sort-Routine.
           MOVE "N" TO Swap-Occur
           PERFORM 400-Secondary-Bub-Sort-Routine VARYING SLVal
             FROM 1 BY 1 UNTIL SLVal > Sort-Max.
       400-Secondary-Bub-Sort-Routine.
           IF Spec-Num(2, SLVal) > Spec-Num(2, SLVal + 1)
             ADD 1 TO Bub-Swap-Count
             MOVE "Y" TO Swap-Occur
             MOVE Spec-Num(2, SLVal) TO Bub-Sort-Hold
             MOVE Spec-Num(2, SLVal + 1) TO Spec-Num(2, SLVal)
             MOVE Bub-Sort-Hold TO Spec-Num(2, SLVal + 1)
           END-IF
           ADD 1 TO Bub-Comp-Count.
       500-Top-Comb-Sort-Routine.
           MOVE "Y" TO Swap-Occur
           MOVE 19 TO Sort-Max
           MOVE 20 TO Comb-Static-Size
           MOVE 0 TO Comb-Swap-Count
           MOVE 0 TO Comb-Comp-Count
           PERFORM 600-Main-Comb-Sort-Routine UNTIL
             Sort-Max IS LESS THAN OR EQUAL TO 1
             AND Swap-Occur = "N".
       600-Main-Comb-Sort-Routine.
           COMPUTE Sort-Max = Sort-Max / 1.25
           MOVE "N" TO Swap-Occur
           PERFORM 700-Secondary-Comb-Sort-Routine VARYING SLVal
             FROM 1 BY 1 UNTIL SLVal + Sort-Max > Comb-Static-Size.
       700-Secondary-Comb-Sort-Routine.
           IF Spec-Num(3, SLVal) > Spec-Num(3, SLVal + Sort-Max)
             ADD 1 TO Comb-Swap-Count
             MOVE "Y" TO Swap-Occur
             MOVE Spec-Num(3, SLVal) TO Comb-Sort-Hold
             MOVE Spec-Num(3, SLVal + Sort-Max) TO Spec-Num(3, SLVal)
             MOVE Comb-Sort-Hold TO Spec-Num(3, SLVal + Sort-Max)
           END-IF
           ADD 1 TO Comb-Comp-Count.

There might be some issues with line length, since our compiler doesn't allow it over a certain length to retain compatibility with older COBOL rules, but besides that this should work. I could log into VMWare for my college and use the school compiler from home, but they all understand the system is slow enough without funneling through the internet. I tried it before, and it took maybe 5-10 minutes to load my USB (I can't load files directly off my computer). Then every normal action took 2-3 minutes to run through, even though running the actual actions locally shouldn't be this terrible. Point being, as long as they say I can add those zeros to the array, I'll use this.

Edit: I think I'll make an Initialization paragraph to shorten the Top Sort Routines a little. I could also reduce the length by merging the swap and comparison counts, as well as the hold, but I feel like it'd be better to keep those for reference instead of purging them after displaying.

On second thought, since most of the MOVEs in those areas are specific and I want to keep them for reference, initializing them in a single paragraph probably would make the code more cluttered. As for Sort-Max, adding a paragraph just for that is dumb. Never mind on it. Just keeping for elaboration of keeping those individualized.
« Last Edit: February 10, 2013, 07:07:34 PM by MegaScientifical »

I know asking this won't help you but

Why are you being taught a language that is outdated and almost never used anymore? (From what I am currently aware of)

COBOL is still used by old banks and such which can't afford mistakes while transferring over to another language. (That's what I've been told.) He actually gives us the option to write some programs in C, but I do NOT want to do two different languages at once. I will DEFINITELY forget up one time and write an entire program in the wrong, unaccepted language. I don't want to forget up like that, considering due dates are strict and such.

Point being, I think this is the last COBOL class. Then I can move on to Java or C or something. I could write SOME programs in C, but since I can't do ALL of them, I'd rather not risk accidentally doing an entire program in C only to discover it was supposed to be in COBOL.


Goddamn, COBOL is ugly.
This.

I don't understand why COBOL uses so many lines.

I remember some time ago there was a topic mentioning something about division or something, I don't quite remember. Anyway, there were users who made their own programs to.do the exact same thing, except in different languages like C++, Python, C#, VB.NET, Java, Lua, etc..

As I recall, one of the users posted the code in COBOL, which was 2 to 4 times as long as what the other users had posted.

Not my fault. This is required, probably because it's relatively close to basic English so stupid people understand it. I'll be moving onto a more fitting coding course next semester, hopefully.

Not my fault. This is required, probably because it's relatively close to basic English so stupid people understand it. I'll be moving onto a more fitting coding course next semester, hopefully.
Actually, I know a programming language and I'm having trouble comprehending what you have here.

Then again the programming language I know is VB.NET, so I probably shouldn't be talking.

I know VB and Java, and I dabble in Torque/Ruby/Python and I don't have a goddamn clue what any of this means.


I know VB and Java, and I dabble in Torque/Ruby/Python and I don't have a goddamn clue what any of this means.
I guess COBOL's syntax is one of those syntaxes that stick out like a sore thumb in modern syntaxes.
Like Assembly, for example.

You get used to it after like a year. :panda: