Vous êtes sur la page 1sur 9

SOLUTIONS TO REVIEW QUESTIONS I. 1. 2. 3. 4. True-False Questions T T F F OCCURS may only be used on levels 02-49.

An OCCURS may be used in either the FILE SECTION or the WORKING-STORAGE SECTION. A table entry may be changed.

5. 6. 7. 8. 9. 10. 11. 12.

F T T T F F T F

SEARCH ALL means that a binary search should be made. An index is initialized by a SET statement.

The program must initialize the index. Only binary searches automatically initialized the index,

II. 1.

General Questions 01 INVENTORY-RECORD. 05 INVENTORY-RECORD-ENTRY 10 10 PART-NUMBER QUANTITY-ON-HAND OCCURS 25 TIMES INDEXED BY X1. PIC X(3). PIC 9(3).

2.

SET X1 TO 1 SEARCH INVENTORY-RECORD-ENTRY AT END PERFORM 300-NO-MATCH-RTN WHEN PART-NUMBER (X1) = 128 MOVE QUANTITY-ON-HAND (X1) TO QUANTITY-ON-HAND-OUT PERFORM 400-WRITE-ANSWER-RTN END-SEARCH. MOVE 0 TO TOTAL-QUANTITY-ON-HAND PERFORM VARYING X1 FROM 1 BY 1 UNTIL X1 > 25 ADD QUANTITY-ON-HAND (X1) TO TOTAL-QUANTITY-ON-HAND END-PERFORM DIVIDE TOTAL-QUANTITY-ON-HAND BY 25 GIVING AVERAGE-QUANTITY-ON-HAND ROUNDED. 01 WS-INVENTORY-TABLE. 05 WS-INVENTORY-RECORD 10 10 WS-PART-NUMBER WS-QUANTITY-ON-HAND OCCURS 100 TIMES INDEXED BY X2. PIC X(3). PIC 9(3).

3.

4.

5a.

This solution assumes that the input file contains exactly 100 inventory records. PERFORM VARYING X2 FROM 1 BY 1 UNTIL X2 > 100 READ INVENTORY-FILE AT END DISPLAY 'NOT ENOUGH RECORDS' END-READ MOVE INVENTORY-RECORD TO WS-INVENTORY-RECORD (X2) END-PERFORM.

If there could be fewer than 100 inventory records, then the solution would be changed to the following.
PERFORM VARYING X2 FROM 1 BY 1 UNTIL X2 > 100 OR ARE-THERE-MORE-RECORDS = NO READ INVENTORY-FILE AT END MOVE NO TO ARE-THERE-MORE-RECORDS NOT AT END MOVE INVENTORY-RECORD TO WS-INVENTORY-RECORD (X2) END-READ END-PERFORM. 5b. If ACCEPT was used, the READ/END-READ for the loop with exactly 100 records would be replaced by: DISPLAY 'PART NUMBER (XXX)?' ACCEPT WS-PART-NUMBER (X1) DISPLAY 'QUANTITY ON HAND (999)?' ACCEPT WS-QUANTITY-ON-HAND (X1) The READ/END-READ for the case where there could be fewer than 100 records would be replaced by: DISPLAY 'PART NUMBER (XXX)?' ACCEPT WS-PART-NUMBER (X1) DISPLAY 'QUANTITY ON HAND (999)?' ACCEPT WS-QUANTITY-ON-HAND (X1) DISPLAY 'IS THERE MORE DATA (YES/NO)?' ACCEPT ARE-THERE-MORE-RECORDS

6.

MOVE 0 TO TOTAL-QUANTITY-ON-HAND PERFORM 700-INCREASE-TOTAL-QOH VARYING X2 FROM 1 BY 1 UNTIL X2 > 100 DIVIDE TOTAL-QUANTITY-ON-HAND BY 100 GIVING AVERAGE-QUANTITY-ON-HAND ROUNDED. 700-INCREASE-TOTAL-QOH. ADD WS-QUANTITY-ON-HAND (X2) TO TOTAL-QUANTITY-ON-HAND.

7.

SEARCH is used when the table is relatively small and/or the data is not in sequence. The SEARCH will check each element in the table in sequence. SEARCH ALL is used when the table is ordered by a key field. The SEARCH ALL performs a binary search on the table data.

8.

MOVE 0 TO WS-LARGEST-POP MOVE 99999999 TO WS-SMALLEST-POP PERFORM 200-COMPARE-RTN VARYING WS-SUB FROM 1 BY 1 UNTIL WS-SUB > 50 END-PERFORM. 200-COMPARE-RTN. IF STATE-POP (WS-SUB) > WS-LARGEST-POP MOVE STATE-POP (WS-SUB) TO WS-LARGEST-POP END-IF IF STATE-POP (WS-SUB) < WS-SMALLEST-POP MOVE STATE-POP (WS-SUB) TO WS-SMALLEST-POP END-IF.

9.

MOVE ZERO TO STATE-CTR PERFORM 200-COMPARE-RTN VARYING WS-SUB FROM I BY 1 UNTIL WS-SUB > 50 END-PERFORM MOVE STATE-CTR TO DL-STATE-CTR WRITE REPORT-RECORD FROM DETAIL-LINE AFTER ADVANCING 1 LINE. 200-COMPARE-RTN. IF STATE-POP (WS-SUB) < 2250000 ADD 1 TO STATE-CTR END-IF.

10.

PERFORM 200-COMPARE-RTN VARYING WS-SUB FROM 1 BY 1 UNTIL WS-SUB > 50 END-PERFORM. 200-COMPARE-RTN. IF STATE-POP (WS-SUB) > 2250000 MOVE WS-SUB TO DL-STATE-NUMBER WRITE REPORT-RECORD FROM DETAIL-LINE AFTER ADVANCING 1 LINE END-IF.

11.

MOVE 0 TO WS-LARGEST-POPULATION PERFORM 200-COMPARE-RTN VARYING WS-SUB FROM 1 BY 1 UNTIL WS-SUB > 50 END-PERFORM MOVE STATE-NAME (WS-LARGEST-STATE-SUB) TO DL-STATE-NAME WRITE REPORT-RECORD FROM DETAIL-LINE AFTER ADVANCING 1 LINE. 200-COMPARE-RTN. IF STATE-POP (WS-SUB) > WS-LARGEST-POPULATION MOVE WS-SUB TO WS-LARGEST-STATE-SUB MOVE STATE-POP (WS-SUB) TO WS-LARGEST-POPULATION END-IF.

12a.

MOVE 1 TO WS-SUB SEARCH STATE-FACTS AT END DISPLAY 'STATE NOT FOUND' WHEN STATE-NAME (WS-SUB) = 'WYOMING' MOVE STATE-POP (WS-SUB) TO DL-STATE-POP WRITE REPORT-RECORD FROM DETAIL-LINE AFTER ADVANCING 1 LINE END-SEARCH. MOVE STATE-POP (50) TO DL-STATE-POP WRITE REPORT-RECORD FROM DETAIL-LINE AFTER ADVANCING 1 LINE

12b.

12c.

Assuming that the state names are in alphabetical order, revise the table so that the 05 level is coded: 05 STATE-FACTS OCCURS 50 TIMES ASCENDING KEY STATE-NAME INDEXED BY STATE-INDEX.

In the PROCEDURE DIVISION, code: SEARCH ALL STATE-FACTS WHEN STATE-NAME (STATE-INDEX) = WYOMING MOVE STATE-POP TO DL-STATE-POP WRITE REPORT-RECORD FROM DETAIL-LINE AFTER ADVANCING 1 LINE END-SEARCH.

III. 1.

Validating Data The program should include routines to verify that: a. b. c. d. SSNO-IN and SALARY-IN are numeric. CAMPUS-CODE-IN is in the range 1 - 5. TITLE-CODE-IN is in the range 001 - 986. DEPT-CODE-IN is in the range 01 - 96.

2.

A control listing of totals should include: a. b. c. The number of personnel records processed. The number of personnel records containing errors. A list of each error found in the personnel file.

3. IV. 1a.

A batch total should be included for SALARY-IN. Internet/Critical Thinking Questions In order to use a binary search, the following criteria must be met: (1) the table must ordered by the search argument, and (2) the search condition is restricted to equality. An additional recommendation is that a table used with a binary search contain at least 50 rows of data in order to justify the overhead involved with this type of search. An internal table is one whose values are hard-coded into the program using VALUE clauses; these values may only be changed by modifying the program code itself and recompiling the program. Internal tables, then, should be restricted to those that are highly unlikely to change. An example is a table containing the number of days in each of the 12 months or the names of the 12 months. An external table is one whose contents are stored in an external file and are loaded into the computers memory during program execution. This type of table is used when the tables contents are likely to change. When such changes occur, they are made in the external file rather than in the program code. This eliminates the need to recompile the program every time the tables contents change. A serial search is the only option available when either of the following conditions is true: (1) the table is not ordered on the search argument, and/or (2) the search condition requires a relation other than equality. The serial search is additionally recommended in cases in which the table entries can be ordered so that the first rows are the ones encountered most frequently; when a serial search is performed on such a table, processing time is minimized because in most cases the matching row will be found at the top of the table.

1b.

1c.

1d.

1e.

A direct-referenced table is one in which no search argument is needed because there is a readily-available subscript for accessing the tables entries. For example, to locate data stored in a table containing information about the 12 months, we know even without searching that the information for the 4th month is located in the 4th row. The number of the month, then, may be used directly as the subscript for accessing the contents of the table. Parallel tables are two (or more) tables whose corresponding rows are directly related to one another. For example, suppose that there are two tables which store information about the 50 states: one contains the names of the capitals and the other holds the names of the governors.

1f.

While all of this information could certainly be stored in a single table, any program that requires use of only one of the two columns would need to store unneeded information in the computers memory. Further, since the names of the capitals will most likely remain the same but the names of the governors will change on a regular basis, the first (capitals) could be defined as an internal table while the second (governors) would best be created as an external table. In either case, it would be better to have two tables, one for capitals and one for governors. Even when a program requires both types of data, there is no need to search both tables; when, for example, the capital table is searched to find the capital of a certain state, the name of the governor can be found by looking directly in the corresponding row of the second (governor) table.
2. Consider a university system with multiple campuses. Each campus has several divisions, the divisions are divided into colleges, the colleges are divided into departments, and the departments have entries for each faculty member. 01 STATE-SYSTEM. 05 CAMPUS OCCURS 13 TIMES. 10 CAMPUS-DIVISION OCCURS 4 TIMES. 15 COLLEGE OCCURS 6 TIMES. 20 DEPARTMENT OCCURS 10 TIMES. 25 FACULTY-MEMBER OCCURS 30 TIMES. (various sub-entries)

SOLUTIONS TO DEBUGGING EXERCISES la. ARE-THERE-MORE-RECORDS is set to NO when the table is loaded in 200TABLE-ENTRY. Since the flag is not reset once it leaves the 200-TABLEENTRY routine, it remains at NO when PERFORM 300-CALC-RTN UNTIL THERE-ARE-NO-MORE-RECORDS is executed. Thus, 300-CALC-RTN is not executed at all. SUB1, the subscript, has a PIC 9 clause. This means that it can only vary from 1 to 9. Since there are 20 entries to be loaded into the table, SUB1 is varied from 1 to 20. Once the subscript reaches a value of 9 and one is added to it, the tens digit is truncated and SUB1 becomes 0. The first statement that tries to use the subscript with a zero value will cause an interrupt because the value is outside the range of the table. To correct the error, SUB1 must have a PIC 99 clause. Code SET Xl TO 1 prior to the SEARCH. If there is no match, the index X1 will contain a value outside the range of the table when the SEARCH statement is completed. The first MOVE statement will then try to move a value outside the table and will cause a program interrupt. Instead of CONTINUE, the first statement following the SEARCH should be included within the SEARCH statement. Therefore, the SEARCH statement should read as follows: SEARCH INV-ENTRIES AT END MOVE 0 TO QTY-OUT WHEN PART-NO-IN = T-PART-NO (X1) MOVE T-QTY-ON-HAND (X1) TO QTY-OUT END-SEARCH. 2c. The search statement should be coded SEARCH TAB1. The 05 entry must include INDEXED BY X1.

lb.

2a. 2b.

Vous aimerez peut-être aussi