How to establish a series of items using an occurs clause. How to access and manipulate data stored in an array or table. Rules for using an occurs clause in the data division




Yüklə 151.48 Kb.
tarix14.04.2016
ölçüsü151.48 Kb.
SINGLE-LEVEL ARRAYS AND TABLES

OBJECTIVES:

. HOW TO ESTABLISH A SERIES OF ITEMS USING AN OCCURS CLAUSE

. HOW TO ACCESS AND MANIPULATE DATA STORED IN AN ARRAY OR TABLE

. RULES FOR USING AN OCCURS CLAUSE IN THE DATA DIVISION

. USE OF SEARCH AND SEARCH ALL FOR TABLE LOOK UPS


INTRODUCTION TO SINGLE-LEVEL OCCURS CLAUSES

OCCURS CLAUSE IS USED TO MODEL (REPRESENT) THE REPEATED OCCURRENCE OF FIELDS WITH THE SAME FORMAT.


MOTIVATION:

GIVEN STUDENTS WITH 20 GRADES TO AVERAGE....

01 INREC.

05

05 GRADE-1 PIC 999.

05 GRADE-2 PIC 999.

...

05 GRADE-20 PIC 999.


TO ADD / AVERAGE THESE DATA ITEMS, WE=D HAVE TO SAY:
COMPUTE AVG = (GRADE-1 + GRADE-2 + ... + GRADE-20)/20.

HOW ABOUT 50 GRADES?

ALL GRADES ARE SAME FORMAT: PIC 999.

WE HAVE 20 AOCCURRENCES@ OF THE SAME FORMAT, SO...



01 INREC.

05 {OTHER STUFF}

05 GRADE OCCURS 20 TIMES PIC 999.

05 {MORE STUFF...}


. CAN USE ON INPUT RECORDS, PRINT RECORDS, OUTPUT RECORDS, ...,

. ANY RECORD HAVING REPEATED OCCURRENCES OF LIKE DATA ITEMS.


. NOW WE HAVE 20 OCCURRENCES OF AGRADE@

. EACH IS 999, THUS 60 BYTES OF STORAGE USED....


HOW TO WE REFERENCE THE INDIVIDUAL GRADES?


. ENTER THE ASUBSCRIPT@

==> AN INTEGER / INTEGER VARIABLE WHOSE VALUE IS USED TO INDICATE WHICH OF THE 20 GRADES WE=RE TALKING ABOUT.


. THUS: MOVE GRADE (5) TO ...

MOVE GRADE (1) TO ....

ADD 10 TO GRADE (15) ...
. RANGE: 1-20

.. CANNOT SAY GRADE (0) OR GRADE (27)

.. CAN SAY

MOVE 10 TO GRADE (X) WHERE X IS AN INTEGER VARIABLE WITH VALUE: 1  X  20

THUS, CAN USE A VARIABLE AS A SUBSCRIPT.
. USE AS A NORMAL VARIABLE IN SOURCE CODE
. ADD 10 TO GRADE (15)  (no spaces within (15)

(space required between AE@ and A(A )




HOW ABOUT VARIABLES AS SUBSCRIPTS?
. VARIABLE USED AS A SUBSCRIPT MUST HAVE A NUMERIC PIC CLAUSE AND AN INTEGER VALUE.
. E.G.: ADD GRADE (X) TO SUM WHERE X IS PIC 999 .


CONSIDER THE CODE:
MOVE 0 TO SUM.

MOVE 0 TO X.

PERFORM 2000-ADD-GRADES UNTIL X > 20.

.....


2000-ADD-GRADES.

ADD 1 TO X.

ADD GRADE (X) TO SUM.

DISPLAY AX HAS VALUE A X. (OR)

DISPLAY ATHIS IS THE A X@TH ITERATION OF THIS PARAGRAPH@

999-NEXT-PARA.

....

HOW ABOUT: MOVE 0 TO SUM

PERFORM 2000-ADD-GRADES VARYING X FROM 1

BY 1 UNTIL X > 20.

....

2000-ADD-GRADES.



ADD GRADE (X) TO SUM.


OR

MOVE 0 TO SUM



PERFORM VARYING X FROM 1 BY 1 UNTIL X>20.

ADD GRADE(X) TO SUM

END-PERFORM

COMPUTE AVG = SUM / 20.

(OR AX-1" ) ... (WHY NOT AVG = SUM / X ????)



EXAMPLE 1:

WRITE A ROUTINE THAT SCANS TWENTY TEST GRADES AND POINTS TO THE HIGHEST GRADE AND ALSO PRINTS OUT WHICH TEST NUMBER THE GRADE WAS RECEIVED IN. (TEST # 14 WAS HIGH; THE GRADE WAS 97%)

(FOR SIMPLICITY, ASSUMES ALL GRADES UNIQUE - FOR NOW)

GIVEN: 01 STUDENT-GRADE-FILE.

05 ...

05 ST-GRADE OCCURS 20 TIMES PIC 999.



05 ...

MOVE 0 TO WS-HIGH-GRADE

MOVE 0 TO WS-TEST-NUMBER

PERFORM 2000-FIND-BIG

VARYING SUB FROM 1 BY 1 UNTIL SUB>20.

PERFORM 3000-PRINT-SEARCH-RESULTS.

...

2000-FIND-BIG.



IF ST-GRADE(SUB) > WS-HIGH-GRADE

MOVE GRADE(SUB) TO WS-HIGH-GRADE

MOVE SUB TO WS-TEST-NUMBER

END-IF.


067

089

097

079



084


  

GRADE (1) GRADE(4) GRADE(20)






CAN SAY:
SEARCH FOR FIRST GRADE VALUE EQUAL TO OR EXCEEDING

80 AND STOP.
ONLY SEARCH GRADES 4 THRU 16. GIVE TEST NUMBER ALSO.

MOVE 0 TO WS-HALT-SEARCH

PERFORM 2000-FIND-BIG VARYING Y FROM 4 BY 1

UNTIL Y > 16 OR WS-HALT-SEARCH = 1.

IF WS-HALT-SEARCH = 1

DISPLAY APOSITION OF HIGH GRADE IN TABLE IS: A Y-1

DISPLAY AHIGHEST GRADE IS: A GRADE(Y-1).

IF WS-HALT-SEARCH NOT = 1

DISPLAY ANO GRADE >= 80 WAS FOUND IN TABLE. A

.....


2000-FIND-BIG.

IF GRADE(Y) >= 80

MOVE 1 TO WS-HALT-SEARCH

END-IF.






OR

MOVE 0 TO WS-HALT-SEARCH

PERFORM 2000-FIND-BIG VARYING Y FROM 4 BY 1

UNTIL Y > 16 OR WS-HALT-SEARCH = 1.

.....
2000-FIND-BIG.

IF GRADE(Y) >= 80

MOVE 1 TO WS-HALT-SEARCH

DISPLAY AVALUE OF FIRST HIGH GRADE IS GRADE(AY@)@

END-IF.
NOTE: VALUE OF Y IS NOT CHANGED UNTIL PARAGRAPH IS EXITED!!




EXAMPLE 2:
LET=S READ IN AGRADE RECORDS@ AND STORE THE AVERAGE FOR EACH RECORD IN A TABLE IN WORKING STORAGE FOR ALL 50 STUDENTS IN THE CLASS (20 GRADES PER STUDENT).



FD STUDENT-GRADE-FILE.

01 ST-GRADE-REC.

....

05 GRADE OCCURS 20 TIMES PIC 999.




WORKING-STORAGE SECTION.

77 SUM PIC 999 VALUE 0. {RECOGNIZE THESE?????}

77 X PIC 99 VALUE 0. {WHY SHOULD YOU KNOW THESE?}

77 Y PIC 99 VALUE 0.

01 AVERAGES.

05 AVG OCCURS 50 TIMES PIC 999V99.

01 HEADER-PRINT-RECORD.

.....
READ STUDENT-GRADE-FILE

AT END .....

PERFORM 1000-BUILD-TABLE VARYING X FROM 1 BY 1

UNTIL X > 50 OR EOF = 1 {IF APPROPRIATE}

...

1000-BUILD-TABLE.

MOVE 0 TO SUM

PERFORM 2000-CALC-AVG VARYING Y FROM 1 BY 1 UNTIL Y > 20

COMPUTE AVG(X) = SUM /20 (OR Y-1)

READ STUDENT-GRADE-FILE

AT END

MOVE 1 TO EOF

END-READ.

2000-CALC-AVG.

COMPUTE SUM = SUM + GRADE(Y).

3000-NEXT-PARA.

HOW ABOUT OUTPUT RECORDS??


EXAMPLE 3:

(READ AND PRINT EXAMPLE)

. PRINT INDIVIDUAL GRADES PER STUDENT WITH THREE SPACES IN BETWEEN EACH GRADE.

. UNKNOWN NUMBER OF STUDENT GRADE RECORDS....

01 P-DETAIL-REC.

....

05 P-OUTPUT-GRADES OCCURS 20 TIMES.



10 P-GRADES-OUT PIC ZZ9.99.

10 PIC XXX.

05 P-THE-REST-OF-THE-STUFF.

.......


. SIZE OF TABLE??

. FIT IN PRINTLINE?

. WHAT WILL IT LOOK LIKE...DESCRIBE....
01 ST-GRADE-RECORD.

....


05 GRADE OCCURS 20 TIMES PIC 999.
PICTURE:

INPUT RECORD:


067

089

097

079



084


  

GRADE (1) GRADE(4) GRADE(20)

b79.25

bbb

b84.69

bbb

b87.20

bbb

-

b74.85

bbb


  FILLER 

P-GRADES-OUT(1) P-GRADES-OUT(3) P-GRADES-OUT(20)

READ INFILE

AT END

MOVE 1 TO F-EOF



END-READ.

MOVE SPACES TO P-DETAIL-REC

PERFORM 2000-READ-PRINT

UNTIL F-EOF = 1

END-PERFORM

........


2000-READ-PRINT.

PERFORM 2100-MOVE-DATA VARYING X FROM 1 BY 1 UNTIL X > 20

WRITE P-DETAIL-REC

READ INFILE

AT END

MOVE 1 TO F-EOF



END-READ

2100-MOVE-DATA.

MOVE GRADE(X) TO P-GRADES-OUT(X)

3000-NEXT-STUFF.

......
DISCUSS.....


(CARRIED FORWARD FROM LAST PAGE)
01 P-DETAIL-REC.

....

05 P-OUTPUT-GRADES OCCURS 20 TIMES.

10 P-GRADES-OUT PIC ZZ9.99.

10 PIC XXX.

05 P-THE-REST-OF-THE-STUFF.

.......

01 ST-GRADE-RECORD.

....

05 GRADE OCCURS 20 TIMES PIC 999.




PROCESSING DATA STORED IN AN ARRAY
USING OCCURS WITH REDEFINES AND VALUE CLAUSES
. CANNOT REDEFINE AN OCCURS ENTRY

. COBOL 74:

05 ITEM-X OCCURS 4 TIMES PIC S999.

05 ITEM-Y REDEFINES ITEM-X PIC X(12)
. BUT CAN DO JUST THE OPPOSITE!!
01 MONTH-NAMES.

05 STRING-1 PIC X(36) VALUE AJANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC@. 05 MONTH REDEFINES STRING-1 OCCURS 12 TIMES PIC XXX.



THUS, READ IN A FIELD WITH CONTENTS EQUAL TO 4 INTO AMN@ AND, MOVE MONTH(MN) TO OUTMONTH.
. MOVES AAPR@ HAD: 01 INREC.

.....

05 MN PIC 99.

....
RULES FOR OCCURS WITH REDEFINITIONS AND VALUES:
. OCCURS MUST AOCCUR@ LAST

. CAN=T HAVE AN OCCURS AT THE 01 LEVEL

(WILL DISCUSS LATER)


Consider:
01 TOTAL1.

05 State.

10 State-Name Occurs 50 times Pic X(10).

10 State-Pop Occurs 50 times Pic 9(10).


Parallel Arrays.

Draw.
In contrast:


01 TOTAL2.

05 State Occurs 50 Times.

10 State-name Pic X(10).

10 State-pop Pic 9(10).


One array.

Draw






. . .







. . .



STATE-NAME STATE-POP

(50)(50)
















STATE-NAME(1) STATE-NAME(2) STATE-NAME(50)

STATE-POP(1) STATE-POP(2) STATE-POP(50)
USING AN OCCURS CLAUSE FOR TABLE HANDLING

TABLES AND ARRAYS - STORED IN THE SAME WAY, BUT USED FOR DIFFERENT PURPOSES.


TABLES ARE USED IN LOOK-UPS AND USUALLY HAVE A "TABLE LOOK-UP" ROUTINE ASSOCIATED WITH IT.
EXAMPLES INCLUDE: TAX TABLES, RATE TABLES, PRICING TABLES, ETC.
TABLES - LISTS OF "STORED" VALUES; REFERENCE VALUES.

ARRAYS TYPICALLY STORE DATA AND TOTALS TO BE OUTPUTTED (OR INPUTTED).


MORE TRANSIENT IN NATURE.

BUT IT IS COMMON TO USE THE TERMS INTERCHANGEABLY...




LETS LOOK AT ANOTHER EXAMPLE OF BUILDING A TABLE FROM INPUT RECORDS.
WE NEED A ZIP CODE TABLE AND A SALES TAX RATE TABLE.

THESE NEED TO BE "PARALLEL" TABLES:


FD INFILE.

01 INREC.

...

05 ZIP-CODE PIC 99999.



05 TAX-RATE PIC V999.

WORKING-STORAGE SECTION.

....

01 TABLES.



05 ZIP OCCURS 100 TIMES PIC 99999.

05 TAX OCCURS 100 TIMES PIC V999.




BUILD TABLE:
READ INFILE

AT END


MOVE 1 TO F-EOF

END-READ


PERFORM 200-BUILD-TABLES VARYING X FROM 1 BY 1

UNTIL F-EOF=1.

....
200-BUILD-TABLES.

MOVE ZIP-CODE TO ZIP (X)

MOVE TAX-RATE TO TAX(X)

READ INFILE

AT END

MOVE 1 TO F-EOF



END-READ
DISCUSS
|___|___|___|32224|_______________________________________|____|

1 2 ZIP TABLE 100


|___|___|___|0.75_|_______________________________________|____|

1 2 TAX TABLE 100




. TABLES ARE BUILT!
. NOW, WE NOTE THAT WE BUILD TABLES SO THAT WE MAY SEARCH THEM MANY TIMES.
. ENTER: A ZIP CODE (CALLED A SEARCH ARGUMENT)
. SEARCH TABLE; TRY TO GET A HIT (MATCH) ON ZIP (TABLE ARGUMENT).
. IF WE GET A HIT, RETRIEVE THE TABLE FUNCTION/TABLE ARGUMENT (OBJECTIVE OF SEARCH)

FD TRANSACTION-FILE

....

01 TRANS-REC.



...

05 ZIP-IN

...


SEARCH:


MOVE 0 TO HIT

READ TRANSACTION-FILE

AT END

MOVE 1 TO F-EOF



END-READ

(more code)...

PERFORM 100-SEARCH VARYING X FROM 1 BY 1

UNTIL X > 100 OR HIT = 1. (RECALL TABLE SIZE=100)

(more code)...

________________________________________________

100-SEARCH.

IF ZIP-IN = ZIP(X)

MOVE TAX(X) TO SOME-PRINT-FIELD

MOVE 1 TO HIT.

IF HIT = 1

PERFORM 400-HIT-RTNE.

IF HIT NOT = 1 AND X = 100

PERFORM 500-NOT-FOUND.

.....


400-HIT-ROUTINE.

WRITE PRINTFIELD CONTAINING TAX RATE...


500-NOT-FOUND.

WRITE MESSAGE STATING NO HIT ENCOUNTERED FOR THIS SEARCH ARGUMENT.




WE CAN DO THIS ANOTHER WAY:
HERE, WE WILL SEARCH FOR ALL RECORDS IN FULL INPUT FILE.
MOVE 0 TO HIT

READ TRANSACTION-FILE

AT END

MOVE 1 TO F-EOF



END-READ

PERFORM 050-SEARCH UNTIL F-EOF=1.

...
050-SEARCH.

PERFORM 100-SEARCH VARYING X FROM 1 BY 1

UNTIL X>100 OR HIT = 1.

IF HIT = 1

WRITE PRINTREC CONTAINING TAX RATE...

ELSE


WRITE PRINTREC CONTAINING NO HIT MESSAGE.

MOVE 0 TO HIT.

READ TRANSACTION-FILE

AT END


MOVE 1 TO F-EOF

END-READ.

100-SEARCH.

IF ZIP-IN = ZIP(X)

MOVE TAX(X) TO SOME-PRINT-FIELD

MOVE 1 TO HIT.




INTERNAL VERSUS EXTERNAL TABLES
WE'VE BUILT TABLES FROM INPUT RECORDS.
WE'VE BUILT TABLES FROM INPUT AND KEPT THEM IN WORKING STORAGE FOR PROGRAM DURATION.
HOW ABOUT FILES (TABLES) THAT ARE RESIDENT ON DISK.

EXTERNAL TABLES:

. STORED ON DISK


. CAN CHANGE DATA ON DISK WITHOUT HAVING TO CHANGE THE PROGRAM.
. NICE TO DO BUSINESS THIS WAY:

E.G., ROUTINE TABLES ON DISK, RATE TABLES, ETC.


. THESE TABLES ARE RESIDENT, AND PROGRAMS CAN ACCESS THEM TO DO THEIR PROCESSING.

INTERNAL TABLES:
. E.G., OUR MONTH TABLES - WITHIN PROGRAM.
. DATA RARELY CHANGES (MONTH TABLE CHANGES?)
. HAVE AS PART OF YOUR PROGRAM

NOW, IF WE CHANGE TABLES IN OUR PROGRAMS, WE MUST RECOMPILE PROGRAM.


EXTERNAL TABLES ARE CHANGED ON DISK BY SOME FILE (TABLE) MAINTENANCE PROGRAM.

EXAMPLE OF INTERNAL TABLE IN YOUR PROGRAM THAT YOU MIGHT NEED TO CHANGE:


...

05 TAX-TABLE.

10 FILLER PIC V999 VALUE .005.

10 FILLER PIC V999 VALUE .010.

10 FILLER PIC V999 VALUE .015.

... ... ... ...

10 FILLER PIC V999 VALUE .100.

05 TAX-RATE REDEFINES TAX-TABLE

OCCURS 20 TIMES PIC V999.

. EASY TO CHANGE.


. DOES REQUIRE RECOMPILATION, CLEARLY.
. REASONABLY STATIC.
. ACCESS INDIVIDUAL ELEMENTS BY:

TAX-RATE(X) ... AS USUAL


. NOTE: REDEFINES AFOLLOWS@ THE DATA BEING REDEFINED.
. NOTE: OCCURS >OCCURS LAST= (THAT IS, AAFTER@ OR AWITH@ THE REDEFINES.



SEARCH STATEMENT.
THE SEARCH STATEMENT MAKES OUR LIFE EASY FOR SEARCHING TABLES...SORT OF.

FORMAT:


SEARCH identifier
[AT END imperative statement-1]
WHEN condition imperative-statement-2

NEXT SENTENCE

[END-SEARCH]


GIVEN: 05 TABLE-ENTRIES INDEXED BY X1 OCCURS 1000 TIMES.


10 ZIPCODE PIC 99999.

10 TAX PIC V999.


SEARCH TABLE-ENTRIES

AT END


MOVE 0 TO DL-SALES-TAX

WHEN ZIPCODE(X1) = ZIP-IN

COMPUTE....

COMPUTE...

WRITE....

END-SEARCH.

SEARCH...AT END ENCOUNTERED??? ===> NO MATCH WAS FOUND.
ALWAYS USE AN AT END - EVEN THOUGH IT IS OPTIONAL.

NOTE: TO USE A SEARCH STATEMENT, TWO ADDITIONAL ENTRIES WERE REQUIRED:


1. THE AINDEXED BY@ CLAUSE

(WHICH IS ASSOCIATED WITH THE OCCURS CLAUSE)


2. THE SET STATEMENT.

CONSIDER THE FOLLOWING TABLE:


01 SALES-TAX-TABLE.

05 TABLE-ENTRIES OCCURS 1000 TIMES

INDEXED BY X1 PIC S9(5).
. INDICES WORK LIKE SUBSCRIPTS BUT ARE NOT DEFINED SEPARATELY FROM THE TABLE (LIKE SUBSCRIPTS)
. SUBSCRIPTS ARE MERELY INTEGER VARIABLES WHOSE INTEGER VALUES ARE USED AS SUBSCRIPTS INTO A TABLE.
. INDICES ARE TIED (ASSOCIATED WITH) THE TABLE. NOT DEFINED ELSEWHERE.
. INDICES ARE AUTOMATICALLY DEFINED BY THE COMPILER.
. INDICES ARE, HOWEVER, SET BY THE PROGRAMMER BUT AUTOMATICALLY INCREMENTED BY THE SEARCH STATEMENT.
. PROCESSING USING INDICES IS MUCH MORE EFFICIENT THAN

SUBSCRIPTS. (MORE AHEAD...)



SUBSCRIPTS:


1. DEFINED IN WORKING STORAGE TYPICALLY
2. CAN CHANGE WITH A MOVE, ADD, SUBTRACT, ...
3. CAN USE PERFORM...VARYING TOO
INDICES:
1. DEFINED VIA AN OCCURS CLAUSE
2. CAN BE MODIFIED WITH A PERFORM VARYING
3. CANNOT BE ALTERED VIA AN ADD, MOVE, SUBTRACT, ...




FORMAT: SET index-name-1 TO



UP BY integer-1

DOWN BY

e.g., SET X1 TO 0

SET X1 UP BY 1

SET X1 DOWN BY 1




WE NEED TO SET THE INDEX PRIOR TO A SEARCH STATEMENT
AS IN, SET X1 TO 1.

SEARCH ....




RULE OF THUMB:


PROBABLY BEST NOT TO USE "NEXT SENTENCE" IN THE WHEN CLAUSE OF THE SEARCH.
NOT FORMALLY "WRONG", BUT CAN CAUSE LOGIC ERRORS IF NOT CAREFUL.
ALSO, CANNOT USE NEXT SENTENCE WHEN END-SEARCH

(COBOL-85 and 2002) IS USED. (RECALL THE AIF@ STATEMENT)

NOTE ACCESSING A TABLE BY INDEXING USING A PERFORM IS VALID.
YOU CAN LOAD OR PROCESS A TABLE WITH AN INDEX USING PERFORM...VARYING.



SEARCHING ...VARYING WITH PARALLEL TABLES
E.G.. ZIPCODE TABLE

AND TAX TABLE.


01 TABLE-1.

05 PART-NO OCCURS 50 TIMES PIC 999

INDEXED BY X1.
01 TABLE-2.

05 QTY-ON-HAND OCCURS 50 TIMES PIC 99

INDEXED BY X2.


OR,


01 TABLE-1.

05 PART-NO OCCURS 50 TIMES PIC 999

INDEXED BY X1.

05 QTY-ON-HAND OCCURS 50 TIMES PIC 99

INDEXED BY X2.


SET X1, X2 TO 0.

SEARCH PART-NO VARYING X2

AT END ....

WHEN PART-NO(X1) = PART-NO-IN OF DETAIL-REC

MOVE QTY-ON-HAND (X2) TO OUT-AREA

END-SEARCH.





THE SEARCH ALL STATEMENT
SERIAL SEARCHES:

. START, INCREMENT AND TEST, HIT OR ITERATE UNTIL EOT.


. SEARCHES ENTIRE TABLE.
. NEEDED WHEN DATA IS IN RANDOM ORDER
. SOMETIMES TABLES ORGANIZED WITH HIGHER PROBABILITIES OF HITS OCCURRING FIRST - TO MINIMIZE COMPARISONS.

MANY TIMES, HOWEVER, TABLES ARE ORDERED IN SOME SEQUENCE: NUMERIC, ALPHABETIC, ...


GIVEN A TABLE:
01 TABLE-1.

05 DISCOUNT-TABLE OCCURS 50 TIMES INDEXED BY X1.

10 T-CUSTOMER-NO PIC 9999.

10 T-DISCOUNT-PCT PIC V999.


IF TABLE IS LARGE AND ORDERED, IT IS TERRIBLY INEFFICIENT TO SEARCH A TABLE SEQUENTIALLY.
AVERAGE NUMBER OF PROBES IS N/2 TO HIT.
TABLE SIZE = 1000 AND AORDERED@ ON SOME FIELD.

PERFORMING A LINEAR SEARCH => MIGHT TAKE 1000 PROBES

TO AFIND@ OR ANOT FIND.@



ENTER BINARY SEARCH....

ASSUME A TABLE CONTAINS 50 ELEMENTS


"BEGIN BY COMPARING ACUST-NO-IN@ OF THE INPUT CUSTOMER RECORD TO THE MIDDLE TABLE ARGUMENT - FOR AT-CUSTOMER-NO@, THAT IS, THE 25TH ENTRY.

SINCE T-CUSTOMER-NOs ARE IN SEQUENCE,


TEST ACUST-NO-IN@ MAY BE > T-CUSTOMER-NO (25).
IF TRUE, THEN WE HAVE ELIMINATED SEARCHING THE FIRST HALF OF THE TABLE!
NEXT COMPARE ACUST-NO-IN@ TO T-CUSTOMER-NO(37), THE MIDDLE TABLE ARGUMENT FOR THE SECOND HALF OF THE TABLE (ROUNDING DOWN) AND CONTINUE COMPARISONS IN THIS WAY.
IF CUST-NO-IN < T-CUSTOMER-NO(25),

HAVE ELIMINATED UPPER HALF OF TABLE.


NEXT COMPARE ACUST-NO-IN@ TO AT-CUSTOMER-NO(12);@

THAT IS, WE DIVIDE THE TOP HALF OF THE TABLE INTO TWO SEGMENTS AND CONTINUE OUR COMPARISONS IN THIS WAY.



THE BINARY SEARCH IS COMPLETE IF EITHER


1. A MATCH HAS BEEN MADE, THAT IS,

CUST-NO-IN = T-CUSTOMER-NO(X1), OR


2. WHEN THE TABLE HAS BEEN COMPLETELY SEARCHED

WITHOUT A MATCH.



FOR TABLES LARGE ENOUGH AND ORDERED, A BINARY SEARCH IS MUCH FASTER THAN TRHE SERIAL SEARCH.


IN THE BINARY SEARCH, SUCCESSIVELY DIVIDING THE TABLE IN HALF.
NORMALLY, WILL HAVE <= log2 n PROBES TO FIND.
GIVEN n = 64. 26 = 64.

SAY: TARGET ITEM IS LOCATED AT POSITION #63 IN TABLE:

PROBE 1: (1 + 64)/2 = 32; RESULT IS >.

PROBE 2: (32+64)/2 = 48; RESULT IS >.

PROBE 3: (48+64)/2 = 56; RESULT IS >.

PROBE 4: (56+64)/2 = 60; RESULT IS >.

PROBE 5: (60+64)/2 = 62; RESULT IS >.

PROBE 6: (62+64)/2 = 63; HIT.

SIX PROBES. 26=64 (MAXIMUM NUMBER OF PROBES)

SAY TARGET ITEM IS LOCATED AT TABLE POSITION #46:

PROBE 1: (1+64)/2 = 32; RESULT >

PROBE 2: (32+64)/2 = 48; RESULT <

PROBE 3: (32+48)/2 = 40; RESULT >

PROBE 4: (40+48)/2 = 44; RESULT >

PROBE 5: (44+48)/2 = 46; HIT!!

FIVE PROBES TO AFIND@ OR AHIT@


WITH LINEAR SEARCH, AVERAGE NUMBER OF PROBES IS N/2.

WITH BINARY SEARCH, MAX IS <= LOG2 N


HOW ABOUT N = 74 ENTRIES AND TARGET IS AT 52.

PROBE 1: (1+74)/2 = 37.5 W/TRUNCATION IS 37. RESULT >

PROBE 2: (37+74)/2 =55.5 W/TRUNCATION IS 55. RESULT <

PROBE 3: (37+55)/2 = 46 RESULT >

PROBE 4: (46+55)/2 W/TRUNC = 50 RESULT <

PROBE 5: (50+55)/2 W/TRUNC = 52 RESULT HIT!!!!

FIVE PROBES TO HIT!! (LINEAR SEARCH? 52 PROBES TO HIT!)


TABLE SIZE = 1000?


FROM: 210 = 1024.
==> MAXIMUM NUMBER OF PROBES TO FIND/NOT FIND IS 10!
DO IT!!
ASSUME: 614 IS OUR TARGET:
PROBE1 = 500; >;

PROBE2 = 750; <;

PROBE3 = 625; <;

PROBE4 = 587; >;

PROBE5 = 606; >;

PROBE6 = 616; <;

PROBE7 = 611; >;

PROBE8 = 614. BINGO. 8!


TABLE MUST BE ORDERED AND BE OF SUFFICIENT SIZE:


IS THERE A ACUTOFF@ UNDERWHICH A SEQUENTIAL SEARCH IS BETTER? YES!!



50


time



(QUITE OLD DATA...BUT GET THE IDEA...)







FORMAT OF SEARCH ALL

SEARCH ALL identifier-1

[AT END imperative-statement-1]




data-name-1 IS EQUAL TO identifier-2



WHEN IS = literal-1

arith expr-1 condition-name

data-name-1
AND

condition-name



imperative-statement-2



NEXT SENTENCE
[END-SEARCH]

. DON'T NEED A ‘SET’ STATEMENT, SINCE THE COMPUTER SETS THE INDEX TO THE APPROPRIATE POINT INITIALLY.


. EXAMPLE:

SEARCH ALL DISCOUNT-TABLE

AT END

PERFORM 500-ERROR-RTNE



WHEN

T-CUSTOMER(X1) = CUST-NO-IN

MULTIPLY AMT-OF-PURCHASE-IN BY

T-DISCOUNT-PCT(X1) GIVING DISCOUNT-AMT-OUT

END-SEARCH.



LIMITATIONS OF SEARCH ALL
1. CAN ONLY TEST FOR EQUALITY WITHIN THE WHEN

WHEN A < B NO!

WHEN A = B OKAY.
2. IF THE CONDITION FOLLOWING THE WORD WHEN IS A COMPOUND CONDITIONAL,

A. EACH PART CAN ONLY USE THE RELATIONAL TEST =

B. CAN ONLY USE "ANDs" TO CONNECT (NOT "Ors")
VALID: WHEN S-AMT(X1) = AMT1

AND TAX-AMT(X1) = AMT2


INVALID: WHEN SALES-AMT(X1) = AMT3

OR AMT4 = AMT5.


3. WITH SEARCH ALL, CAN ONLY USE ONE "WHEN"
4. VARYING CANNOT BE USED IN A SEARCH ALL
5. OCCURS ITEM AND ITS INDEX MUST APPEAR TO THE LEFT OF THE EQUAL SIGN.

VALID: WHEN S-AMT(X1) = AMT1 ...


INVALID: WHEN AMT1 = S-AMT(X1) ....
6. MUST HAVE TABLE ORDERED ON A KEY TOO
"ASCENDING" OR "DESCENDING"




SIMPLE SEARCH:
01 TABLE.

05 INVENTORY-TABLE OCCURS 100 TIMES

INDEXED BY ITEM-IND.

10 IT-ITEM-NO PIC 9999.

10 IT-ITEM-DESCRIPTION PIC X(10).

10 IT-ITEM-PRICE PIC 9999V99.


SET ITEM-IND TO 1.

SEARCH INVENTORY-TABLE (OCCURS CLAUSE...)

AT END ... (ELSE)

WHEN ... (IF...THEN...)

END-SEARCH.

BINARY SEARCH:
01 TABLE.

05 INVENTORY-TABLE OCCURS 100 TIMES

INDEXED BY ITEM-IND

ASCENDING KEY IT-ITEM.

10 IT-ITEM-NO PIC 9999.

10 IT-ITEM-DESCRIPTION PIC X(10).

10 IT-ITEM-PRICE PIC 9999V99.
SEARCH ALL INVENTORY-TABLE

AT END ...

WHEN ...

END-SEARCH-ALL.






SUBSCRIPTS INDEX

PIC DEFINED INDEXED BY CLAUSE IN TABLE

OR USAGE IS INDEX WITH NO PIC
ANY COMMAND; ARITHMETIC SET, SET UP, SET DOWN, SEARCH

STATEMENTS; MOVE; SEARCH ALL, PERFORM...VARYING

PERFORM...VARYING

VALUES: SIZE OF ARRAY VALUES OF 0 TO MAX NUMBER OF

BYTES IN TABLE.

POSITION OF ENTRY IN DISPLACEMENT OF ENTRY FROM

TABLE START OF TABLE - IN BINARY








Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azrefs.org 2016
rəhbərliyinə müraciət

    Ana səhifə