View previous topic :: View next topic
|
Author |
Message |
nareshdacha
New User
Joined: 12 Jan 2010 Posts: 66 Location: US
|
|
|
|
I have a input file which contains the values as emp_no,emp-name,emp-sal. Each employee can contain more than once but emp_no is unique.
My requirement is to calucate the salary of each employee using SEARCH only.
For example:
emp-no e-name emp-sal
100 xxx 1000
200 abc 2000
300 pqr 3000
100 xxx 4000
300 pqr 1000
Now result should be like this
Emp-no emp-sal
100 5000
200 2000
300 4000
If emo_no exists more than once then the salary should be added togetherand display.
Can anyone explain how better way we can code this. |
|
Back to top |
|
|
Binop B
Active User
Joined: 18 Jun 2009 Posts: 407 Location: Nashville, TN
|
|
|
|
Hi Naresh,
Quote: |
Can anyone explain how better way we can code this |
Shouldn't you be tellling us what your design is ... so that we can suggest a "better" way (if any)... |
|
Back to top |
|
|
arvind.m
Active User
Joined: 28 Aug 2008 Posts: 205 Location: Hyderabad
|
|
|
|
you can do it with sort i guess....you can search in the forum, you'll find many. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
When help is asked for a programming solution, it is rarely appropriate to propose a utility solution.
Most often there is more to the requirement than is posted and adding additional processes (the cobol code will most likely still be needed to do the complete process) just wastes resources and is additional maintenance. . .
@Naresh - you can accomplish what you want by using an internal sort and summarizing by emp-no. If you get stuck, post what you have done and where you want help - as Binop suggested. |
|
Back to top |
|
|
nareshdacha
New User
Joined: 12 Jan 2010 Posts: 66 Location: US
|
|
|
|
Actually this question was asked in interview.. She asked to me to explain with the code by using COBOL only.. i have pasted the code for your ref...
I just pasted a rough code below.... Please suggest me whether this process is correct or not..... Or any other way ????
First i loaded the input values to a table EMP-TABLE
05 EMP-TABLE OCCURS 10 TIMES INDEXED BY I
10 EMP-NO PIC 9(5)
10 EMP-NAME PIC X(10
10 EMP-SAL PIC 9(4)
05 WS-TABLE OCCURS 10 TIMES INDEXED BY J
10 WS-NO PIC 9(5)
10 WS-NAME PIC X(10
10 WS-SAL PIC 9(4)
10 WS-SUM PIC 9(8)
READ INPUT FILE (INP-NO,INP-NAME,INP-SAL) AT END GO TO END-PARA
LOAD THE INPUT VALUES TO EMP-TABLE,WS-TABLE..........
SET I TO 1
SET J TO 1
PERFORM PARA1 10 TIMES
ADD 1 TO J
PARA1.
SEARCH ACCT-TABLE
NOT FOUND DISPLAY 'ERROR'
WHEN WS-NO(J) = EMP-NO (I)
WS-SUM(I) = WS-SUM(I) + EMP-SAL(I)
ADD 1 TO I
IF I = 10 THEN
MOVE 1 TO I
END-SEARCH |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
You are inconsistent. ADD, MOVE, SUBTRACT, etc, cannot be used against an INDEX. Only SET is allowed.
Bill |
|
Back to top |
|
|
nareshdacha
New User
Joined: 12 Jan 2010 Posts: 66 Location: US
|
|
|
|
Ya you are correct.....its my mistake....
Then how to loop thru the file to check the current record and prev record to add the salary........... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
This would be better done using the COBOL "internal sort" (especially if the file has more than just a few records).
Read about the COBOL SORT in the Language Reference manual available via the "IBM Manuals" link at the top of the page. If you find something in the manual that is not clear, post what you found and your doubt and someone will be able to clarify. |
|
Back to top |
|
|
nareshdacha
New User
Joined: 12 Jan 2010 Posts: 66 Location: US
|
|
|
|
Yes dick ... I replied the same to the interviewer but she said ' I want in COBOL itself' hence i tried as above............. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Yes, you can use arrays and increment the values according to when the "key" value is found.
Suggest you actually convert your pseudo-code to real code and run the process with a bit of made-up inpt data. |
|
Back to top |
|
|
Ajay Baghel
Active User
Joined: 25 Apr 2007 Posts: 206 Location: Bangalore
|
|
|
|
First you can sort the input file data by empno using sort statement. Populate the table. Now use the below code
Search-para.
SAL-TOTAL = 0
SET NDX TO 1
HOLD-EMP = emp(1)
Perform proces-para
process-para.
search table at end
DISPLAY HOLD-EMP, SAL-TOTAL
NEXT SENTENCE
when emp(NDX) = HOLD-EMP
compute sal-total = sal-total + sal(NDX)
set ndx up by 1
go to process-para
when emp(NDX) not equal HOLD-EMP
DISPLAY HOLD-EMP, SAL-TOTAL
MOVE ZERO TO SAL-TOTAL
SET HOLD-EMP TO EMP(NDX)
compute sal-total = sal-total + sal(NDX)
set ndx up by 1
go to process-para
END-SEARCH
Thanks
Ajay |
|
Back to top |
|
|
Ajay Baghel
Active User
Joined: 25 Apr 2007 Posts: 206 Location: Bangalore
|
|
|
|
Code: |
Search-para.
Move zero to SAL-TOTAL .
SET NDX TO 1.
Move emp(1) to HOLD-EMP.
Perform proces-para.
process-para.
search table at end
DISPLAY HOLD-EMP, SAL-TOTAL
NEXT SENTENCE
when emp(NDX) = HOLD-EMP
compute sal-total = sal-total + sal(NDX)
set ndx up by 1
go to process-para
when emp(NDX) not equal HOLD-EMP
DISPLAY HOLD-EMP, SAL-TOTAL
MOVE ZERO TO SAL-TOTAL
SET HOLD-EMP TO EMP(NDX)
compute sal-total = sal-total + sal(NDX)
set ndx up by 1
go to process-para
END-SEARCH |
|
|
Back to top |
|
|
|