Mar 21, 2010

Table Handling

1. Runs scored by 15 players into 25 matches needs to be stored into array. What kind of array is required to store the same - single or multidimensional? Define an array.

Array can be defined as
01   PLAYER-DATA.
      05 PLAYER-NAME OCCURS 15 TIMES INDEXED BY PL-INDEX PIC X(25).
          10 MATCH-RUNS OCCURS 25 TIMES INDEXED BY RUN-INDEX.
                15  MATCH RUN   PIC 9(3).

To store runs of 25 matches for 15 players, two dimensional array will be required. PLAYER-NAME stores name of player which occurs 15 times and it has been defined as X(25). Also for each player, MATCH-RUNS has been defind which occurs 25 times.

2. In above two dimensional array, if runs scored by SACHIN in each match needs to be displayed, how it can be done?

Using Search
Serial search can be used. First Player Name = "SACHIN" is found using PL-INDEX. Once it is found then using PL-INDEX, all the run scored by SACHIN can be found out.

SET PL-INDEX TO 1
SET RUN-INDEX TO 1
SEARCH PLAYER-DATA
    WHEN PLAYER-NAME(PL-INDEX) = "SACHIN                   "
               DISPLAY 'PLAYER FOUND'
    AT END
               PERFORM P100-EXIT
END-SEARCH

SET PL-INDEX DOWN BY 1.

PERFORM UNTIL RUN-INDEX < 25
   DISPLAY PLAYER-NAME (PL-INDEX,RUN-INDEX)
END-PERFORM.

P1000-EXIT.
  EXIT.


Using Perform loop
This task is quite easy using perform loop

SET PL-INDEX TO 1
SET RN-INDEX TO 1
PERFORM UNTIL END-OF-TABLE
       IF PL-INDEX = 15
             SET END-OF-TABLE TO TRUE

IF PLAYER-NAME(PL-INDEX) = "SACHIN                  "

PERFORM UNTIL RUN-INDEX < 25

      DISPLAY PLAYER-NAME (PL-INDEX,RUN-INDEX)
END-PERFORM
SET END-OF-TABLE TO TRUE

END-IF
END-PERFORM

3. What is sequential search and Binary search ?
In Sequential search, table elements are searched sequentially using SEARCH verb. When table element is found, control comes out of search. If table element is not found, action defined at AT END verb is performed.

In Binary search, table must be ordered(sorted in ascending or descending) on the KEYS specified in the occurs clause.The test in when condition must be always equal condition(same is not true with Sequential search. In sequential search we can have unequal conditions in WHEN). Also we can have only one when condition in binary search (Whereas in sequential search, we can have mutilple when conditions)

In binary search data is search using  SEARCH-ALL verb. Table will be devided into two parts and key will be tested. If the key to be searched lies in first part, then second part will be elliminated and search will continue in first part.Process will repeat until key is found or no specified key found in table.

If no specified key found, then action specified at AT END clause is performed.

4. Sequential search or Binary search  - When to use what?
Serial search is used if table to be searched is relatively small. Binary search is more efficient if size of table to be searched in big.

Also for binary search, table needs to have key and data must be in sorted order on a key. If the data is not like this , then serial search is performed.

5. Can SEARCH and SEARCH ALL be used to find records in a sequential file?
SEARCH and SEARCH all cannot be used to find data in sequental file directly. They can be used with tables which are declared in the program in DATA division.

However, file records can be loaed into table and they can be searched.

6. What is difference between index and subscript ?
Index and subscript are used for refering a data element into table.

A subscript is a working storage data definition item, typically an interger PIC (999) where a value must be moved to the subscript and then incremented or decremented by ADD TO and SUBTRACT FROM statements. Subscript represents an occurence number of a table within a table.

An index is a register item that exists outside the program's working storage. You SET an index to a value and SET it UP BY value and DOWN BY value. An index is displacement from the start of table based upon length of table element.

index values can be changed by SET, PERFORM and SEARCH.

7. A table needs to be defined to store the names of students in the class. Maximum number of students in class can be 100. Table needs to be defined to store names of only those students who have taken admissions. Number of students who have taken admission can be retrieved from WS-ADM-COUNT. Define an table.

Table can be defined in following way

01. WS-ADM-COUNT PIC 9(2).
01. WS-STUDENTS.
    05. WS-STUDENT-NAME OCCURS 1 TO 100 TIMES DEPENDING ON WS-ADM-COUNT
                                              PIC X(25).

With above definition, WS-STUDENTS will be variable length table. Depending upon value in WS-ADM-COUNT, occurences of WS-STUDENT-NAME will be set.

if WS-ADM-COUNT has value 50, then above table will have data for 50 occurences or allocate data into storage for 50 occurences.

8. How to set an WS-INDEX to 25? How to set WS-INDEX to WS-INDEX-2

 To set WS-INDEX to 25  -> SET WS-INDEX TO 25
 To set WS-INDEX to WS-INDEX-2 -> SET WS-INDEX DOWN BY 2.      

9. If 10 Dimension table needs to be defined in COBOL? Define any 10 dimensional table?

  Tables upto 7 dimensions can be defined in COBOL

10. A two dimensional array has been defined in following way. What it does?

  01. WS-TABLE OCCURS 10 TIMES PIC X(3).
        05 WS-TABLE2 OCCURS 5 TIMES PIC X(5).

Above definition is wrong, since OCCURS clause cannot be specified at 01 level.If two dimensional array is required, above defintion can be modified as

 01. WS-TABLE-GROUP.
    05 WS-TABLE OCCURS 10 TIMES PIC X(3).
         10 WS-TABLE2 OCCURS 5 TIMES PIC X(5).

                                                                                                            BACK TO COBOL




              
         

No comments:

Post a Comment