Download presentation
Presentation is loading. Please wait.
Published byValerie Brooks Modified over 8 years ago
1
Array Applications
2
Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design an algorithm that searches a table using a binary search.
3
Objectives (Continued) Design an algorithm that makes use of one or more program switches. Design an algorithm that sorts values in a table.
4
We Have Seen How To... organize collections of similar data items into a group instead of referring to them each as unique, individual items. use subscript notation to refer to specific elements of a group. design algorithms to process sets of values (smallest, average, etc.).
5
Table Lookups Loading a table - reading a table (multi- dimensional array) into computer storage. Table load usually occurs in the initialization module. Once loaded, the table can be referred to again and again during processing. One example might be a table of wage classes and pay rates.
6
Table Lookup (Continued) Input record contains employee pay class and hours worked. Look for wage class in table to find pay rate for employee pay class. This process of searching for a value in a table is called table lookup.
7
Table Lookup Example Input a two-dimensional table of products and prices. Then, input records will contain an item number and quantity. If item found, compute price. If not, print error message.
8
Solution Structure chart: OVERALL CONTROL A000 PROCESS TABLE LOOKUP B010 INPUT REFRENCE TABLE B000
9
Overall Control (A000) Start Input reference table (B000) READ NUMBER, QUANT IF NUMBER = 99999 THEN Write ‘No data for me to look up!’ ELSE DOWHILE NUMBER <> 99999 Process table lookup (B010) READ NUMBER, QUANT ENDDO WRITE ‘End of job’ ENDIF Stop
10
Input Reference Table B000 Enter ROW = 1 EOF = 0 DOUNTIL EOF = 1 READ ITEM#/PRICE(ROW,1), ITEM#/PRICE(ROW,2) IF ITEM#/PRICE(ROW,1) = 99999 THEN NCNT = ROW - 1 EOF = 1 ELSE IF ROW = 200 THEN NCNT = 200 EOF = 1 ELSE ROW = ROW + 1 ENDIF ENDDO Return
11
Item#/Price Table After input, the table looks something like this: Item#/Price Table COLUMNS 1 2 118337 50.85 235795 5.85 311427 1.50 Rows 498547105.50 554387 10.95 677378 3.50 748591 14.95 ……… 199unusedunused 200unusedunused
12
Table Lookup (Continued) DOUNTIL in the Input Reference Table module (B000) controls loading of the table. Note the use of nested IFTHENELSE structures because we are not sure how many rows of data will be loaded. (We do not know ahead of time how many items we have.) The program allows for up to 200 items and their prices in the table. If less than 200 items are input, a trailer record with an item number of 99999 is used.
13
Table Lookup (Continued) If the 200th row is reached, the special value EOF is set to 1. Likewise, if we reach the trailer record before row 200, we subtract one from the row number (so the trailer record is not counted) and again set EOF to 1.
14
Program Switches In this case, the DOUNTIL loop is controlled by the variable named EOF. EOF is a program switch - a value set up to deal with special conditions that may arise during processing. Implementation varies depending on need. In this case, EOF = 0 (off) at start and then “turned on” (set to 1) when there are no more records to be loaded into the table.
15
Table Load Processing No matter how we terminate the table load (trailer record or 200 records, whichever comes first) the variable NCNT is set to the number of rows in the table before EOF is set. We can then use NCNT in subsequent processing of the table (such as searching).
16
Back to Overall Control After table is loaded, we return to the overall control module (A000) and begin processing input records for inquiries against our table. We use a trailer record again (item number of 99999) to indicate the end of the inquiries. We also make sure there really are inquiries to process.
17
Process Table Lookup B010 Enter ROW = 1 DONE = 0 DOWHILE DONE = 0 IF ITEM#/PRICE(ROW,1) = NUMBER THEN PRICE = ITEM#/PRICE(ROW,2) * QUANT WRITE NUMBER, QUANT, ITEM#/PRICE(ROW,2), PRICE DONE = 1 ELSE ROW = ROW + 1 IF ROW > NCNT THEN WRITE ‘No data available for’, NUMBER DONE = 1 (ELSE) ENDIF ENDDO Return
18
Process Table Lookup Main processing is controlled by a DOWHILE loop. One of two conditions will stop execution of the loop (by changing the switch DONE to 1): –Input item number matches ITEM#/PRICE(ROW,1). –The end of the table is reached (NCNT rows processed) without a match.
19
Process Table Lookup (Continued) Nested IFTHENELSE control structures test both conditions (table entry matches input, or we have processed NCNT rows without a match). If a match is found, information is printed and DONE is set to 1 (to stop the loop). If no match is found, test for number of rows processed (ROW > NCNT).
20
Process Table Lookup (Continued) If the item number in (ROW,1) does not match the input, we increment ROW by 1, then check its value against NCNT. If ROW > NCNT we did not find a match and a message is written to that effect. DONE is set to 1 to stop the loop. Otherwise, we go on to the next row.
21
Binary Searches We need a more sophisticated search routine when: –frequency of use of table entries is evenly distributed over the table. –the number of entries in the table is very large. –processing efficiency or high system performance is necessary. Binary search is commonly employed for these situations.
22
Binary Search (Continued) In order for binary search to work correctly, the values in the table must be in ascending or descending order by one or more fields common to all records. This field is called a key field. Binary refers to the fact that the effective size of the table is halved repeatedly. Search begins at or near middle of table.
23
Binary Search (Continued) If not a match, search continues at or near the middle of the upper or lower half of the table which would contain the item. Other half of table is ignored. Process continues, halving the size of the table remaining. Eventually we either find a match or we cannot halve the table any more.
24
Binary Search Example A company has 70 sales offices, each identified by a unique two-digit code. However, when reports are produced the sales office location is to be used instead of its code. Office codes and locations are input to the program in ascending order and stored in separate lists. (That is, we load two separate lists.) These lists are then used to locate an office code which is also input (for report processing).
25
Binary Search Solution OVERALL CONTROL A000 PROCESS INPUT B020 SEARCH TABLE B010 INPUT TABLE B000 Structure Chart:
26
Overall Control A000 Start Input Table (B000) IF VALID-TABLE = “YES” THEN Read INCODE IF INCODE = 00 THEN WRITE “No codes to process” ELSE DOWHILE INCODE <> 0 Search Table (B010) Process a detail record (B020) Read INCODE ENDDO ENDIF …
27
Overall Control (Continued) ELSE Write ‘Table not in sequence.’ ENDIF Stop
28
Input Table (B000) B000 Enter READ CODE(1), LOCATION(1) SUB = 1 VALID-TABLE = “YES” DOWHILE SUB < 70 SUB = SUB + 1 READ CODE(SUB), LOCATION(SUB) IF CODE(SUB-1) > CODE(SUB) THEN VALID-TABLE = “NO” SUB = 70 (ELSE) ENDIF ENDDO Return
29
Search Table (B010) B010 Enter FOUND = “NO” FIRST = 1 LAST = 70 DOWHILE FIRST < LAST AND FOUND = “NO” MID = INT((FIRST + LAST) / 2) IF INCODE = CODE(MID) THEN FOUND = “YES” SAVECODE = CODE(MID) SAVELOCATION = LOCATION(MID) ELSE …
30
Search Table (B010) (Continued) IF INCODE < CODE(MID) LAST = MID – 1 ELSE FIRST = MID + 1 ENDIF ENDDO Return
31
Sorting Lists In the binary search example we checked the input for the table to make sure the input values were in order. We could have input them in any order and then used a sort algorithm to put the table entries in order. Before we look at sorting, though, we need to look at exchanging two values in the computer’s memory - because that’s what sorting algorithms do.
32
Exchanging Values Exchanging Values Incorrectly: 5353 NUM1 NUM2 Step 1.NUM1 = NUM2 2.NUM2 = NUM1
33
Exchanging Values (Incorrectly) Step 1. NUM1 NUM2 5 3 3 Step 2. NUM1 NUM2 5 3 3
34
Exchanging Values Without Arrays Step 1. TEMP = NUM1 2. NUM1 = NUM2 3. NUM2 = TEMP (Illustrated on page 266 and on board)
35
Exchanging Values With Arrays LIST TEMP 8 7 5 3 9 Step 1.TEMP = LIST(4) 2.LIST(4) = LIST(3) 3.LIST(3) = TEMP See illustration on page 267
36
Sort Example Problem: Sort a list of four values into ascending order, and output the sorted list. Solution: Read the items into a group (list). The value in position 1 of the list is compared to the value in position 2. If the first value is larger than the second one, they are interchanged. Then positions 3, 4, …, n are compared to the first value and exchanged if necessary. At this point the first pass is complete and position 1 contains the smallest value.
37
Sort Example (Continued) We begin with the second position and compare positions 3, 4, …, n to position 2. This is the second pass. At the end of the second pass, position 2 is known to contain the second-smallest value. Continue with this method until we have completed n-1 passes. At that point the list will be in sorted order. Structure chart follows...
38
Structure Chart for Sort OVERALL CONTROL A000 INPUT ARRAY B000 SORT ARRAY B010 OUTPUT ARRAY B020 EXCHANGE VALUES C000
39
Overall Control A000 Start Input Array (B000) Sort Array (B010) Output Array (C000) Stop
40
Sort Array (B010) B010 Enter TOT = 4 PASS = 1 DOUNTIL PASS = TOT SUB = PASS + 1 DOUNTIL SUB > TOT IF LIST(PASS) > LIST(SUB) THEN Exchange Values (C000) (ELSE) ENDIF SUB = SUB + 1 ENDDO PASS = PASS + 1 ENDDO Return
41
Exchange Values (C000) C000 Enter TEMP = LIST(SUB) LIST(SUB) = LIST(PASS) LIST(PASS) = TEMP Return
42
Sort Example This example uses four values, but with slight modification could be used to sort more than four values. This is not the only algorithm for sorting - there are in fact many ways to sort lists of numbers, some of which are more efficient than others. It is almost a certainty that you will see more sort problems if you take more programming classes!
43
Assignment 5 Chapter 11 Page 272 Exercise 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.