CHAPTER 12
ARRAY PROCESSING AND
TABLE HANDLING
CHAPTER OBJECTIVES
Upon
completion of this chapter, the student should be able to:
1. Describe the purpose of the OCCURS
clause.
2. Demonstrate the use of single-level
arrays and tables.
3. Explain the term subscript and describe
its use.
4. Describe the purpose of the SEARCH
statement.
5. Describe the purpose of an index and
how to use it.
6. Explain how the INDEXED BY clause is
used with the SEARCH statement.
7. Explain how the SEARCH ... VARYING option
is used for processing.
8. Describe how the SEARCH ALL statement
is used to perform a binary search.
9. Explain how the ASCENDING/DESCENDING
KEY clause is used.
10.
Describe criteria used to determine
whether to use a serial or binary search.
11. Explain the differences between serial
and binary searches.
12. Describe how to establish a series of
items using a double-level OCCURS clause.
13. Explain how to look up data in a
double-level or two-dimensional array.
14. Explain how to print data stored in a
double-level or two-dimensional array.
LECTURE OUTLINE
1. An Introduction to Single-Level OCCURS
Clauses
A. Why OCCURS Clauses Are Used
1. An OCCURS clause is used to indicate
the repeated occurrence of fields with the same format.
2. An OCCURS clause may be used:
a. To define a series of input or output
fields that have the same format.
b. To define a series of totals in
WORKING-STORAGE to which amounts are added.
c. To define a table in WORKING-STORAGE
that will be accessed by each input record.
Normally, the contents of some input field is used to "look
up" the required data in the table.
3. A subscript is used in the PROCEDURE
DIVISION to indicate which specific item within the array is to be accessed.
4. Coding rules for subscripts are:
a. A subscript must be enclosed within
parentheses. The left parenthesis must
be preceded by at least one space, and the right parenthesis must be followed
by at least one space or a period.
b. There should be no spaces within the
parentheses.
c. A subscript may be a positive integer
or an identifier with a numeric integer PICTURE clause.
d. A subscript can have a relative value
(i.e. a data-name or integer to which another data-name or integer is added or
subtracted). For example: WS-SUB + 5.
5. A PERFORM ... VARYING statement is
commonly used to systematically process each element of an array.
6. An array of totals may be initialized
to zero:
a. With a PERFORM ... VARYING.
b. With an INITIALIZE statement.
c. With a VALUE clause at or below the
level of the OCCURS clause.
7. Input fields that are used as
subscripts must contain valid values.
B. Rules for Use of the OCCURS Clause
1. An OCCURS clause may only be used on
levels 02 - 49.
2. An OCCURS clause may define either
group or elementary items.
II. Processing
Data Stored in an Array
A. Using OCCURS with VALUE and REDEFINES
Clauses
1. Once an entry has been defined by an
OCCURS clause, it may not be redefined.
2. An OCCURS clause may be used to
REDEFINE another entry.
3. COBOL 85 permits a field defined with
an OCCURS clause to also have a VALUE clause.
B. Printing Data Stored in an Array
1. An OCCURS clause can be used in an
output record.
2. To print data stored in an array, use a
PERFORM ... VARYING statement.
3. If several items are to appear on a
single printed line, use the OCCURS clause with a group item and define both
the identifier and a FILLER, or blank field name, as elementary items within
the group item to allow for blank spaces between values.
4. Refer to the text for examples illustrating
how to print tables in a variety of ways.
III. Using
an OCCURS Clause for Table Handling
A. Defining a Table
1. A table is a list of stored fields of
the same type and size that are referenced by the program in conjunction with a
table look-up routine. A table look-up
is a procedure that finds a specific entry in the table.
2. Table data is typically entered from a
file and stored in WORKING-STORAGE.
B. Storing the Table in WORKING-STORAGE
1. Each table entry consists of two types
of fields:
a. The table argument is the table entry
field that is used to locate the desired element.
b. The table function is the element to be
looked up.
2. Table data is usually read from a file
rather than stored as a constant, because this makes changing the table data
easier and more efficient. Table data
may also be entered by using the keyboard.
3. Refer to the text for sample code to
load a table from a file.
C. Looking up Data in a Table: Finding a
Match
1. To look up data in a table, each table
argument is examined until it matches the search argument.
2. The search argument is the input field
that is compared to the table argument during the look-up procedure.
3. When a match is found between the table
argument and the search argument, the corresponding table function with the
same subscript as the table argument is the desired value.
4. This table look-up procedure may be
accomplished with a PERFORM ... VARYING until a match is found or the end of
the table has been reached.
IV. Use of the SEARCH Statement for Table and
Array Processing
A. Format of the SEARCH Statement
1. The best method for searching a table
is with the use of a SEARCH statement.
2. Format of the SEARCH statement:
SEARCH
identifier-1
[AT END
imperative statement-1]
WHEN condition-1
{imperative-statement-2/CONTINUE}
...
[END-SEARCH]
3. The END-SEARCH scope terminator should
be used, and CONTINUE may be coded if necessary.
4. The identifier used is the table entry
name specified on the OCCURS level.
5. The WHEN clause indicates what action
is to be taken when the condition specified is met. This condition compares a search argument
with a table argument.
6. The AT END clause specifies what action
should be taken if the table has been completely searched and no match is
found.
7. To use a SEARCH statement, two
additional entries are required:
a. The INDEXED BY clause in the table
definition.
b. The SET statement in the PROCEDURE
DIVISION.
B. The INDEXED BY Clause and the SEARCH
Statement
1. When using a SEARCH statement, table
entries must be specified with an index rather than a subscript.
2. An index is defined along with the
table entries as part of the OCCURS clause.
For example:
05 TABLE-ENTRIES OCCURS 1000 TIMES
INDEXED BY index-name.
3. An index is not defined separately in
WORKING-STORAGE. The compiler will
automatically provide an appropriate PICTURE for the index.
4. When the SEARCH statement is executed,
the index is automatically incremented until the condition specified is
satisfied or until the AT END condition is met.
5. To start the table look-up at the first
entry, the programmer must initialize the table’s index before each SEARCH.
6. A subscript represents an occurrence of
an array or table element, while an index represents a displacement from the
first address in the array or table.
C. Modifying the Contents of an Index
1. To modify the contents of an index:
a. A PERFORM ... VARYING statement may be
used.
b. A SET statement can be used.
2. An index may not be modified with a
MOVE, ADD, or SUBTRACT statement.
3. The basic format of the SET statement:
SET
index-name-1 {TO/UP BY/DOWN BY} integer-1
4. The SEARCH statement does not
automatically initialize the index at 1.
The index should be initialized to 1 using the SET statement prior to
each execution of a SEARCH.
5. Differences between subscripts and
indexes are summarized in the text.
6. An index can be used to reference an
element only in the table or array for which it was defined.
D. Using Two WHEN Clauses for an Early Exit
from a SEARCH
1. Multiple WHEN clauses are permitted
with the SEARCH statement.
2. If the condition in the first WHEN is
not met, the search will test the next WHEN.
Processing of a SEARCH statement continues until a WHEN condition is
met.
3. When the table arguments are in
sequence but not necessarily consecutive, a second WHEN may be used to
discontinue the search as soon as the table argument exceeds the value of the
search argument.
4. A direct-referenced table can be
accessed directly by using an input field as a subscript, thereby eliminating
the need for a SEARCH.
E. Searching For Multiple Matches
1. Sometimes, a search for multiple
matches in a table is required.
2. A PERFORM ... VARYING statement should
be used when searching a table for more than one match.
F. Internal vs External Tables
1. An external table is stored on disk or
other auxiliary device and is loaded into the program as needed.
2. An internal table is defined in a
program with the use of VALUE clauses.
3. If table entries are expected to remain
fixed, use an internal table. When table
data changes periodically, use an external table.
V. Looking up Table Data for Accumulating
Totals
Refer to the text for
coding examples that use a table look-up in order to accumulate totals.
VI. The SEARCH ... VARYING Option for
Processing Parallel Tables
A. Parallel tables are two tables having
values that correspond to one another.
B. SEARCH ... VARYING is used to search one
table while updating the index for a parallel table so that both tables are
processing corresponding rows.
C. Format of the SEARCH ... VARYING
statement:
SEARCH
identifier-1
VARYING {identifier-2/index-name-1}
[AT END
imperative-statement-1]
{WHEN condition-1
{imperative-statement-2/NEXT SENTENCE}}
...
[END-SEARCH].
VII.
The SEARCH ALL Statement
A. Serial Search
1. The SEARCH statement described up to
this point is a serial search. It is
executed as follows:
a. The first entry in the table is
searched.
b. If the condition is met, the look-up is
completed.
c. If the condition is not met, the index
or subscript is incremented by 1, and the next entry is searched.
d. This procedure continues until a match
is found or the table has been completely searched.
2. A serial search is best used when:
a. The entries in a table are not in order
or,
b. Table entries are organized so that
those most frequently used are the first ones encountered.
3.
Even though a serial search may be used
when the table entries are in some sequence, it is not always the preferred
search method. In the case of large
sequenced tables, the serial search is inefficient because excessive time is
spent locating entries toward the end of the table.
B. The Binary Search as an Alternative to
the Serial Search
1. A binary search is a method of
searching a series of table entries that are in sequence by some key field.
2. A binary search proceeds as follows:
a. Begin by comparing the search argument
to the middle table argument.
b. Determine if the desired table argument
is in the first half or the second half of the table.
c. Eliminate the half of the table that
does not contain the desired table argument.
d. Continue this procedure until a match
is found or the table has been completely searched.
3. On average, a binary search takes fewer
comparisons to find a match than does a serial search.
4. A binary search is preferable to a
serial search when:
a. Table entries are arranged in sequence
by some table field, and
b. The table contains at least 50 entries.
C. Format of the SEARCH ALL Statement
1. The SEARCH ALL is used to perform a
binary search.
2. The format of the SEARCH ALL is the
same as that for the SEARCH except that the WHEN clause may only test an equal
condition. If the WHEN clause tests a
compound condition, only AND is permitted.
3. A SET is not necessary with the SEARCH
ALL. The computer will adjust the index
automatically.
4. Only one WHEN clause can be used with a
SEARCH ALL statement.
5. The VARYING option is not allowed with
the SEARCH ALL statement.
6. The OCCURS item and its index must
appear to the left of the equal sign in the WHEN clause.
D. ASCENDING or DESCENDING KEY with the
SEARCH ALL Statement
1. To use the SEARCH ALL, a key field must
be identified in an ASCENDING or DESCENDING key clause.
2. The ASCENDING or DESCENDING KEY is
specified along with the OCCURS and INDEXED BY clauses of a table entry.
3. The identifier used in the ASCENDING or
DESCENDING KEY clause must be an entry within the table.
4. The KEY clause must appear prior to the
INDEXED BY clause.
1.
Differences Between the SEARCH and SEARCH
ALL Statements:
SEARCH |
SEARCH ALL |
Performs a serial search |
Performs a binary search |
Table entries may be in any order |
Table entries must be in some sequence. The field that is in sequence is specified
in an ASCENDING or DESCENDING KEY clause as part of the OCCURS entry. |
A SET statement is needed to specify the
starting point for the look-up |
Does not need a SET statement prior to
execution |
Can include any relational test (<, >,
=, <=, >=) or any compound conditional in the WHEN clause |
Can only have a single = condition tested with
the WHEN clause. |
May include multiple WHEN clauses |
May have only one WHEN clause |
2.
COBOL 2000+ Changes
a.
VALUE clauses may be used within a table
to initialize every occurrence of an element to the same value.
b.
The INDEXED BY and KEY clauses may be
coded in either order within the definition of the table.
c.
The SORT verb may be used to sort table
entries.
A. Introduction
1. More
than one level of OCCURS may be used in COBOL.
2. Multiple level OCCURS may be used for:
a. Accumulating totals in an array.
b. Storing a table for look-up purposes.
B. Defining a Double-Level or
Two-Dimensional Array
1. Examine the illustration in this
section of the text, which shows how a two-dimensional array is defined and how
it can be visualized in storage.
2. If an item is defined by a double-level
OCCURS clause then it must be accessed by two subscripts:
a. The first subscript refers to the first
or higher-level OCCURS clause.
b. The second subscript refers to the
second or lower-level OCCURS clause.
3. The subscripts must be enclosed in
parentheses. The left parenthesis must
be preceded by at least one space, and the right parenthesis must be followed
by at least one space or a period.
4. Subscripts are separated by a comma and
space.
5. Subscripts may be positive integers or
data-names with positive integer contents.
C. Accessing a Double-Level or
Two-Dimensional Array
1. Two techniques for processing each
element of a two-dimensional array are:
a. Using nested PERFORM statements.
b. Using the PERFORM ... VARYING ... AFTER
statement.
i. The PERFORM ... VARYING varies the
major subscript.
ii.
The AFTER clause varies the minor
subscript.
iii.
This statement requires a procedure name
after the word PERFORM; it cannot be coded as an in-line PERFORM.
2. An element of an array is accessed by
using the lowest level item. The item
must be accessed using subscripts for each level of OCCURS.
3. When PERFORM ... VARYING ... AFTER is
executed, the minor subscript is varied first while the major subscript is held
constant. The major subscript is then
incremented by one and again the minor subscript is varied. This process continues until the major
subscript has been incremented to its highest value.
D. Using a Double-Level or Two-Dimensional
Array for Accumulating Totals
1. Any array used for accumulating totals should
be initialized to zeros.
2. With COBOL 85, an array can be
initialized to zeros by moving zeros to the group item name using a statement
such as:
MOVE
ZEROS TO INVENTORY-TOTALS
3. Do not code MOVE 0
TO INVENTORY-TOTALS because this will only
change the leftmost character to zero; all other characters will be blank. This is because INVENTORY-TOTALS is a group
item and is treated as an alphanumeric field.
4. COBOL 74 will not allow the group-name
of an array to be referenced unless a COBOL 74 compiler with an enhancement is
being used. Thus, ZEROS cannot be moved
to the array. Instead, an initialization
routine is required to initialize the array elements.
5. An input field may be used as a
subscript. If input fields are used,
they should be validated to ensure that they are in the range of the OCCURS
clauses.
6. Refer to the text for a sample program
that:
a. Initializes a two-dimensional array of
accumulators.
b. Updates the elements of the array.
c. Prints the array.
E. Performing a Look-up Using a
Double-Level OCCURS
1. If the SEARCH statement is used, then
INDEXED BY must be used on all OCCURS levels.
2. The SEARCH verb is used with the lowest
level OCCURS entry.
3. The SEARCH statement increments the
lowest level index only. That is, when
used on a two-dimensional table, each SEARCH can search only one row of the
table.
4. To search the entire table, the SEARCH
itself must be executed from a PERFORM ... VARYING that increments the major
index.
5. Alternately, a PERFORM ... VARYING ...
AFTER can be used to search a two-dimensional table.
SOLUTIONS TO REVIEW QUESTIONS
I. True-False Questions
1. T
2. T
3. F OCCURS
may only be used on levels 02-49.
4. F An
OCCURS may be used in either the FILE SECTION or the
WORKING-STORAGE SECTION.
5. F A
table entry may be changed.
6. T
7. T
8. T
9. F SEARCH
ALL means that a binary search should be made.
10. F An
index is initialized by a SET statement.
11. T
12. F The
program must initialize the index. Only
binary searches automatically
initialized
the index,
II. General Questions
1. 01
INVENTORY-RECORD.
05
INVENTORY-RECORD-ENTRY OCCURS 25
TIMES
INDEXED BY X1.
10
PART-NUMBER PIC X(3).
10
QUANTITY-ON-HAND 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.
3.
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.
4. 01
WS-INVENTORY-TABLE.
05
WS-INVENTORY-RECORD
OCCURS 100 TIMES
INDEXED
BY X2.
10
WS-PART-NUMBER PIC X(3).
10
WS-QUANTITY-ON-HAND PIC 9(3).
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.
12b. MOVE
STATE-POP (50) TO DL-STATE-POP
WRITE
REPORT-RECORD FROM DETAIL-LINE
AFTER ADVANCING 1 LINE
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. Validating Data
1. The program should include routines to
verify that:
a. SSNO-IN and SALARY-IN are numeric.
b. CAMPUS-CODE-IN is in the range 1 - 5.
c. TITLE-CODE-IN is in the range 001 -
986.
d. DEPT-CODE-IN is in the range 01 - 96.
2. A control listing of totals should
include:
a. The number of personnel records
processed.
b. The number of personnel records containing
errors.
c. A list of each error found in the
personnel file.
3. A batch total should be included for
SALARY-IN.
IV. Internet/Critical Thinking Questions
1a. 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.
1b. 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.
1c. An external table is one whose contents
are stored in an external file and are loaded into the computer’s memory during
program execution. This type of table is
used when the table’s 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 table’s
contents change.
1d. 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.
1e. A direct-referenced table is one in which
no search argument is needed because there is a readily-available subscript for
accessing the table’s 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.
1f. 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.
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 computer’s 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 200-TABLE-ENTRY. Since the flag is not reset once it leaves
the 200-TABLE-ENTRY 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.
lb. 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 ten’s 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.
2a. Code SET Xl TO 1
prior to the SEARCH.
2b. 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.