View previous topic :: View next topic
|
Author |
Message |
jaffarhussain
New User
Joined: 26 Nov 2010 Posts: 30 Location: Hyderabad
|
|
|
|
I am pasting the code which i have written.
I am struck in the logic to print the records from reverse order.
EX: IF Input data set has:
My name is Jaffar
Output data set should be having:
raffaJ si eman yM
Please share me the logic and solution to get this.
IDENTIFICATION DIVISION.
PROGRAM-ID. REVERSE.
AUTHOR. JAFFAR HUSSAIN.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INDATA ASSIGN TO DD1.
SELECT OUTDATA ASSIGN TO DD2.
DATA DIVISION.
FILE SECTION.
FD INDATA.
01 INREC PIC X(80).
FD OUTDATA.
01 OUTREC PIC X(80).
WORKING-STORAGE SECTION.
01 PRINT.
02 ARRAY PIC X(80) OCCURS 1 TIMES INDEXED BY IDX.
01 X PIC 9(01) VALUE ZERO.
88 END-OF-FILE VALUE 1.
77 I PIC 9(01) VALUE 01.
*77 IDX USAGE IS INDEX.
77 COUNT1 PIC 9(02) VALUE 80.
PROCEDURE DIVISION.
PROGRAM-START.
PERFORM 001-MOVE-PARA.
001-MOVE-PARA.
PERFORM VARYING I FROM 1 BY 1 UNTIL I>1
OPEN INPUT INDATA
OPEN OUTPUT OUTDATA
PERFORM UNTIL END-OF-FILE
READ INDATA
AT END SET END-OF-FILE TO TRUE
END-READ
MOVE INREC TO ARRAY(1)
IF X<1
* PERFORM VARYING IDX FROM 80 BY -1 UNTIL IDX<1
* SET IDX TO 02
* PERFORM UNTIL COUNT1>1
WRITE OUTREC FROM ARRAY(1)
* MOVE -1 TO COUNT1
* DISPLAY 'COUNT1:' COUNT1
* END-PERFORM
* END-PERFORM
END-IF
END-PERFORM
END-PERFORM
CLOSE INDATA
CLOSE OUTDATA
STOP RUN. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
At the top of the page is a link to "IBM Manuals". The top of the list is for COBOL. Look in the Language Reference for your release of the compiler for REVERSE. The flashlight/tubelight near the top left is the search feature for the manual.
You might also look in this COBOL part of the forum as this has been discussed before. |
|
Back to top |
|
|
jaffarhussain
New User
Joined: 26 Nov 2010 Posts: 30 Location: Hyderabad
|
|
|
|
Thanks for your quick response
I have did it using this function reverse before and I got the output.
But for me the task is without using any function i need to write logic of my own.
Else can you give me the open source of function reverse so that I can go thought it what logic it has in function reverse ?
Thanks a lot in advance |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
First, COBOL is not open source.
Second, the function's source code is almost certainly in assembler, written by and maintained by IBM, and not accessible unless you work for IBM in the compiler support group.
Third, there is nothing hard about using reference modification with an input and output variable to reverse the input variable -- so why are you needing source code, anyway? |
|
Back to top |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
Is this a student assignment?
Please use BBcode tags in your posts when you post code. They can also be useful in structuring your text.
You have posted a nice framework above for a start, you only need til fill in the right logic.
Hint:
You need 2 arrays: The target array (the reversed string)
If you have a loop (perform) counting a source index value down from the highest (max) index value until the lowest, the index value of the target array could be expressed as:
Code: |
target-idx = max-idx - source-idx + 1 |
You first challenge is to think and come up with the right algorithms and logic, then the programming should be easy! |
|
Back to top |
|
|
jaffarhussain
New User
Joined: 26 Nov 2010 Posts: 30 Location: Hyderabad
|
|
|
|
I got the solution blow is the code worked sucessfully
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INDATA ASSIGN TO DD1.
SELECT OUTDATA ASSIGN TO DD2.
DATA DIVISION.
FILE SECTION.
FD INDATA.
01 INREC PIC X(80).
FD OUTDATA.
01 OUTREC PIC X(80).
WORKING-STORAGE SECTION.
01 X PIC 9(01) VALUE ZERO.
88 END-OF-FILE VALUE 1.
01 Y PIC 9 VALUE 0.
77 I PIC 99 VALUE 80.
77 J PIC 99 VALUE 01.
01 WS-TEMP PIC X(80).
PROCEDURE DIVISION.
PROGRAM-START.
PERFORM 001-MOVE-PARA.
001-MOVE-PARA.
OPEN INPUT INDATA
OPEN OUTPUT OUTDATA
PERFORM UNTIL END-OF-FILE
MOVE 80 TO I
MOVE 01 TO J
READ INDATA
AT END SET END-OF-FILE TO TRUE
END-READ
IF END-OF-FILE
MOVE 1 TO Y
END-IF
PERFORM UNTIL I < 1
MOVE INREC(I:1) TO WS-TEMP(J:1)
* I = I - 1
* J = J + 1
DISPLAY 'I:' I
DISPLAY 'J:' J
SUBTRACT 1 FROM I
ADD 1 TO J
END-PERFORM
IF Y<1
WRITE OUTREC FROM WS-TEMP
END-IF
END-PERFORM
CLOSE INDATA
CLOSE OUTDATA
STOP RUN. |
|
Back to top |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
Quote: |
Code: |
PERFORM UNTIL I < 1
MOVE INREC(I:1) TO WS-TEMP(J:1)
* I = I - 1
* J = J + 1
DISPLAY 'I:' I
DISPLAY 'J:' J
SUBTRACT 1 FROM I
ADD 1 TO J
END-PERFORM
|
|
My suggestion was to eliminate the use of J completely:
Code: |
PERFORM VARYING I FROM 80 BY -1 UNTIL I < 1
MOVE INREC(I:1) TO WS-TEMP(81-I:1)
DISPLAY 'I:' I
END-PERFORM
|
|
|
Back to top |
|
|
|