1 / 46

Chapter 6

Chapter 6. Using Arrays (Tables). Chapter Outline. What Is an Array (Table)? Example: Array versus Nonarray Processing (Positionally- Organized Arrays) How Do We Put Values into Arrays? Example: Arrays in Validation Programs (Compile-Time Arrays)

andrew
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Using Arrays (Tables) Programming Logic for Business Laura Saret EdD Copyright 2001 Laura Saret

  2. Chapter Outline • What Is an Array (Table)? • Example: Array versus Nonarray Processing (Positionally- Organized Arrays) • How Do We Put Values into Arrays? • Example: Arrays in Validation Programs (Compile-Time Arrays) • Example: Arrays in Validation Programs (Execution-Time Arrays) • What Is the Difference Between a Sequential Search and a Binary Search? • Example: Binary Search • How Do We Work with Multidimensional Arrays? • Example: Finding Values in Two-Dimensional, Argument-Organized Arrays • Example: Values as Part of a Range • How Do Various Programming Languages Differ in How They Work with Arrays?

  3. Chapter Objectives After completing this chapter you should be able to.… • Explain the advantages of using arrays. • Initialize and access entries in one- and two-dimensional arrays using sequential and binary search techniques. • Indicate when data should be stored in compile-time arrays and when they should be stored in execution-time arrays. • Explain how corresponding arrays are used. • Construct the logic for programs that use positonally-organized and argument-organized arrays. • Discuss some of the differences in how various programming languages work with arrays.

  4. January February March April May June July August September October November December What Is an Array (Table)? • A simple variable holds one value • HEADING1 • EMPLOYEE-NAME • TEST-SCORE • COUNTER • The value of the variable or field may change during program execution • Arrays (also called tables)consist of several data values maintained under a common name • Arrays let you easily manipulate large amounts of related data

  5. Third person in line Second person in line First person in line When Using arrays, We Can Refer to Storage Locations by Their Positions

  6. January February March April May June July August September October November December Month-Array • 12 entries or items • Month-Array refers to all 12 items • Month-Array(1) refers to January • Month-Array(6) refers to June • The number in parentheses is called a subscript • Arrays are often called subscripted variables

  7. January February March April May June July August September October November December Subscripts • Can be a number (0,1,2,3,4…) • Can be a field name in which the content of the field is a number • Suppose that Row = 2 • What is Month-Array(Row)? • Suppose that the month (11) is entered by the user and stored in Month-In • What is Month-Array(Month-In)? • Suppose the user enters 14 for the month (Month-In) • What is Month-Array(Month-In)?

  8. Advantages of Arrays Over Simple Variables • No matter how many values are stored, the program only needs one name to refer to the values • Program logic can be shorter and, in many cases, simpler • Instead of using separate field names, loops can be used to reference the items in an array • During each pass of the loop, the subscript changes

  9. Arrays Require that Programmers Do 3 Things • Describe or define the array • Reserve storage • Specify the type of data the array contains • Initialize the array • Put values in array • Access or lookup entries • Find entries in array • Argument-organized versus positionally-organized arrays

  10. Why Use Arrays? • Suppose I am using 3 assignment grades to determine an average • Average = (Assn1 + Assn2 + Assn3) / 3 • What if the average was based on 25 assignment grades? • Average = (Assn1 + Assn2 + Assn3 + Assn4 + Assn5 + Assn6 + Assn7 + Assn8 + Assn9 + Assn10 + Assn11 + Assn12 + Assn13 + Assn14 + Assn15 + Assn16 + Assn17 + Assn18 + Assn19 + Assn20 + Assn21 + Assn22 + Assn23 + Assn24 + Assn25) / 25 • What if the average was based on 100 assignment grades?

  11. Calculate Average of 100 Grades Using an Array • In Initialize routine ASSIGN names and initial values in work area ASSIGNMENT-ARRAY (100 ENTRIES) SUBSCRIPT = 1 SUM = 0 AVERAGE • In Calculate routine LOOP UNTIL SUBSCRIPT > 100 SUM = SUM + ASSIGNMENT-ARRAY (SUBSCRIPT) SUBSCRIPT = SUBSCRIPT + 1 ENDLOOP AVERAGE = SUM / 100 • What changes would have to be made to find the average of 1000 grades?

  12. Assignment-Array is Positionally-Organized • The subscript that points to the array position is… • The same as a field in the input or • The counter in a counter-controlled loop • The example in 6.2 (page 245) also uses a positionally-organized array • The subscript is the same as a field in the input (Stu-No) • Look at the flowchart on page 249-250 • What does the Build-Array routine do? • What does this instruction do? Add Stu-Points To Points-Array (Stu-No)

  13. Do Write-Array-Entry Student: 25 <= Write-Array-Entry > Write Student, Points-Array (Student) Add 1 to Student Return Points-Array is Positionally-Organized • Look at the Loop in Terminate (page 250) • How many times is Write-Array-Entry done? • What is Student? • Look at Write-Array-Entry • What is the subscript?

  14. How Do We Put Values into Arrays? • Execution-time arrays • Values are put into an array during program execution • To modify the array, the input value must be changed • Compile-time arrays • Values are put into an array when the program is written • To modify the array, a programmer must change the program, and it must be retranslated • Disadvantages • Programs that use execution-time arrays take longer to execute than program with compile-time arrays • Changing a compile-time array requires involving a programmer

  15. So, Should You Use a Compile-Time or Execution-Time Array? Compile-time or execution-time? • Depends on frequency of changes • Frequently changed arrays should be defined as execution-time • Grocery store prices • Credit card balances • Infrequently-changed arrays should be defined as compile-time • Mathematical tables • States and zip codes

  16. Example 6.4 (page 252) Uses an Argument-Organized, Compile-Time Array to Validate Input • Why is the array argument-organized? • Why is it compile-time? ? ? ? ? ? ? ?

  17. Let’s Search the Array CUSTOMER-ARRAY(1) = 14516 CUSTOMER-ARRAY(2) = 12725 CUSTOMER-ARRAY(3) = 12825 CUSTOMER-ARRAY(4) = 16265 CUSTOMER-ARRAY(5) = 17172 CUSTOMER-ARRAY(6) = 18291 CUSTOMER-ARRAY(7) = 12000 CUSTOMER-ARRAY(8) = 11400 CUSTOMER-ARRAY(9) = 14421 CUSTOMER-ARRAY(10) = 13222 CUSTOMER-ARRAY(11) = 12546 CUSTOMER-ARRAY(12) = 18931 CUSTOMER-ARRAY(13) = 19211 CUSTOMER-ARRAY(14) = 17645 CUSTOMER-ARRAY(15) = 11001 CUSTOMER-ARRAY(16) = 11235 CUSTOMER-ARRAY(17) = 11762 CUSTOMER-ARRAY(18) = 11984 CUSTOMER-ARRAY(19) = 11111 CUSTOMER-ARRAY(20) = 12156 Raise your hand when you are through searching for customer 11235 Did you find the customer number? Where did you find it? (What was the subscript?)

  18. Let’s Search the Array Again CUSTOMER-ARRAY(1) = 14516 CUSTOMER-ARRAY(2) = 12725 CUSTOMER-ARRAY(3) = 12825 CUSTOMER-ARRAY(4) = 16265 CUSTOMER-ARRAY(5) = 17172 CUSTOMER-ARRAY(6) = 18291 CUSTOMER-ARRAY(7) = 12000 CUSTOMER-ARRAY(8) = 11400 CUSTOMER-ARRAY(9) = 14421 CUSTOMER-ARRAY(10) = 13222 CUSTOMER-ARRAY(11) = 12546 CUSTOMER-ARRAY(12) = 18931 CUSTOMER-ARRAY(13) = 19211 CUSTOMER-ARRAY(14) = 17645 CUSTOMER-ARRAY(15) = 11001 CUSTOMER-ARRAY(16) = 11235 CUSTOMER-ARRAY(17) = 11762 CUSTOMER-ARRAY(18) = 11984 CUSTOMER-ARRAY(19) = 11111 CUSTOMER-ARRAY(20) = 12156 Raise your hand when you are through searching for customer 13847 Did you find the customer number?

  19. The Program Uses 2 Indicators Search-Finished is given an initial value before doing Look-For-Entry • Look at the Process routine on page 254 to see how the indicators are used • Search-Finished • Entry-Found Look-For-Entry must change Search-Finished to “YES”. Why? Entry-Found is given a value in Look-For-Entry

  20. Let’s Look for Customer Number 12825 CUSTOMER-ARRAY(1) = 14516 CUSTOMER-ARRAY(2) = 12725 CUSTOMER-ARRAY(3) = 12825 CUSTOMER-ARRAY(4) = 16265 CUSTOMER-ARRAY(5) = 17172 CUSTOMER-ARRAY(6) = 18291 CUSTOMER-ARRAY(7) = 12000 CUSTOMER-ARRAY(8) = 11400 CUSTOMER-ARRAY(9) = 14421 CUSTOMER-ARRAY(10) = 13222 CUSTOMER-ARRAY(11) = 12546 CUSTOMER-ARRAY(12) = 18931 CUSTOMER-ARRAY(13) = 19211 CUSTOMER-ARRAY(14) = 17645 CUSTOMER-ARRAY(15) = 11001 CUSTOMER-ARRAY(16) = 11235 CUSTOMER-ARRAY(17) = 11762 CUSTOMER-ARRAY(18) = 11984 CUSTOMER-ARRAY(19) = 11111 CUSTOMER-ARRAY(20) = 12156 Customer-Sub = 1 Search-Finished = “NO”

  21. Let’s Look for Customer Number 12825 CUSTOMER-ARRAY(1) = 14516 CUSTOMER-ARRAY(2) = 12725 CUSTOMER-ARRAY(3) = 12825 CUSTOMER-ARRAY(4) = 16265 CUSTOMER-ARRAY(5) = 17172 CUSTOMER-ARRAY(6) = 18291 CUSTOMER-ARRAY(7) = 12000 CUSTOMER-ARRAY(8) = 11400 CUSTOMER-ARRAY(9) = 14421 CUSTOMER-ARRAY(10) = 13222 CUSTOMER-ARRAY(11) = 12546 CUSTOMER-ARRAY(12) = 18931 CUSTOMER-ARRAY(13) = 19211 CUSTOMER-ARRAY(14) = 17645 CUSTOMER-ARRAY(15) = 11001 CUSTOMER-ARRAY(16) = 11235 CUSTOMER-ARRAY(17) = 11762 CUSTOMER-ARRAY(18) = 11984 CUSTOMER-ARRAY(19) = 11111 CUSTOMER-ARRAY(20) = 12156 Customer-Sub = 2 Search-Finished = “NO”

  22. Let’s Look for Customer Number 12825 CUSTOMER-ARRAY(1) = 14516 CUSTOMER-ARRAY(2) = 12725 CUSTOMER-ARRAY(3) = 12825 CUSTOMER-ARRAY(4) = 16265 CUSTOMER-ARRAY(5) = 17172 CUSTOMER-ARRAY(6) = 18291 CUSTOMER-ARRAY(7) = 12000 CUSTOMER-ARRAY(8) = 11400 CUSTOMER-ARRAY(9) = 14421 CUSTOMER-ARRAY(10) = 13222 CUSTOMER-ARRAY(11) = 12546 CUSTOMER-ARRAY(12) = 18931 CUSTOMER-ARRAY(13) = 19211 CUSTOMER-ARRAY(14) = 17645 CUSTOMER-ARRAY(15) = 11001 CUSTOMER-ARRAY(16) = 11235 CUSTOMER-ARRAY(17) = 11762 CUSTOMER-ARRAY(18) = 11984 CUSTOMER-ARRAY(19) = 11111 CUSTOMER-ARRAY(20) = 12156 Customer-Sub = 3 Search-Finished = “NO”

  23. The Example in Section 6.5 (page 258) Uses an Execution-Time Array • Initialize calls Load-Array to read and store values in the array • CUSTOMER-SUB is the subscript • NUMBER-OF-ENTRIES counts the entries in the array • It is initialized to 1 LOAD-ARRAY READ an array entry (CUSTOMER-ENTRY) LOOP UNTIL EOF MOVE CUSTOMER-ENTRY to CUSTOMER-ARRAY (CUSTOMER-SUB) ADD 1 to CUSTOMER-SUB ADD 1 to NUMBER-OF-ENTRIES READ an array entry (CUSTOMER-ENTRY) ENDLOOP ENDLOAD-ARRAY

  24. Look-For-Entry Compares Customer-Sub to Number-Of-Entries Instead of 20

  25. Number of Array Entries Max. Comp. --Binary Search Max. Comp.-- Seq.Search Ave. Comp.-- Seq. Search 10 4 10 5 50 6 50 25 100 7 100 50 200 8 200 100 500 9 500 250 1,000 10 1,000 500 5,000 13 5,000 2,500 10,000 14 10,000 5,000 100,000 17 100,000 50,000 1,000,000 20 1,000,000 500,000 Binary Search • We have been doing sequential searches • Binary searches are faster

  26. Prerequisite for Binary Search • Array elements must be arranged in order

  27. Start with the middle entry 11001 11111 11235 11400 11762 11984 12000 12156 12546 12725 12825 13222 14421 14516 16265 17172 17645 18291 1931 Let’s Look for 12000Using a Binary Search 12000 is less than 12725 so we eliminate the “bottom half” of the array

  28. Look at the middle entry Let’s Look for 12000Using a Binary Search 11001 11111 11235 11400 11762 11984 12000 12156 12546 12000 is greater than 11762 so we eliminate the “top half” of the array

  29. Look at the middle entry Let’s Look for 12000Using a Binary Search 11984 12000 12156 12546 We found it!!!!

  30. Binary Search(page 264)

  31. (1,1) (1,2) (2,1) (2,2) (3,1) (3,2) Working with Multidimensional Arrays • The dimension of the array is the number of subscripts required to access the array • We have been working with 1-dimensional arrays • 2-dimensional arrays • The first subscript is the row • The second subscript is the column

  32. 3-Dimensional Array (1,1,1) (4,2,1) (6,3,2)

  33. WeatherArray 47 25 1.13 13.4 52 30 1.56 14.0 62 39 2.71 15.1 72 49 2.77 14.9 79 58 5.22 13.3 87 67 4.31 12.4 93 71 2.61 11.4 93 70 2.60 10.9 84 62 3.84 11.7 74 50 3.23 12.3 60 39 1.98 13.2 50 29 1.40 12.9 Weather-Array Ave. High Temp. Ave. Low Temp. Ave. Precipitation Ave. Wind

  34. Number Name Code 12987 Ronald Gupta 2 29801 Sonal Desai 1 29991 Miriam Abrams 3 30987 Carol Burns 4 44563 Eric Johnson 5 48723 Peter Legardo 1 56560 Alison Gaynor 3 57341 Diana Chin 3 59992 Michael Cernowitz 2 60132 Marla Kaplan 4 Sales-Array What is Sales-Array (1,3)? What is Sales-Array (4,2)? What is Sales-Array (8,5)?

  35. Number Name Code 12987 Ronald Gupta 2 29801 Sonal Desai 1 29991 Miriam Abrams 3 30987 Carol Burns 4 44563 Eric Johnson 5 48723 Peter Legardo 1 56560 Alison Gaynor 3 57341 Diana Chin 3 59992 Michael Cernowitz 2 60132 Marla Kaplan 4 Sales-Array If you know a salesperson’s number, how do you find his her or name? How do you find his or her code?

  36. Number Name Code 12987 Ronald Gupta 2 29801 Sonal Desai 1 29991 Miriam Abrams 3 30987 Carol Burns 4 44563 Eric Johnson 5 48723 Peter Legardo 1 56560 Alison Gaynor 3 57341 Diana Chin 3 59992 Michael Cernowitz 2 60132 Marla Kaplan 4 Sales-Array If the salesperson’s number is in row ROW-SUB, where is his or her name? Code?

  37. Let’s Look at the Example in Section 6.9 (page 267) • How is the array defined? (page 270) • How is the billing calculated? (page 271) PROCESS MOVE 1 to ROW-SUB MOVE “NO” to SEARCH-FINISHED LOOP UNTIL SEARCH-FINISHED = “YES” DO LOOK-FOR-ENTRY routine ENDLOOP IF ENTRY-FOUND = “NO” THEN DO WRITE-ERROR routine ELSE GROSS-BILLING = BILLING-ARRAY(ROW-SUB,2) * HOURS-BILLED DO WRITE-DETAIL routine ENDIF READ a record (EMP-NUMBER, EMP-NAME, BILL-CODE, HOURS-BILLED) ENDPROCESS

  38. Total Points Grade 370-400 A 360-369 A- 330-359 B 320-329 B- 291-319 C 280-290 C- 251-279 D 240-250 D- 0-239 F What Happens When Values Are Part of a Range? • See example on page 275

  39. Total Points Grade 370-400 A 360-369 A- 330-359 B 320-329 B- 291-319 C 280-290 C- 251-279 D 240-250 D- 0-239 F What Happens When Values Are Part of a Range? • The array contains the lowest value in each range • F is not in the array 370 A 360 A- 330 B 320 B- 291 C 280 C- 251 D 240 D-

  40. 370 A 360 A- 330 B 320 B- 291 C 280 C- 251 D 240 D- LOOK-FOR-ENTRY IF TOTAL-POINTS >= GRADE-ARRAY (ROW-SUB,1) THEN MOVE “YES” to SEARCH-FINISHED MOVE “YES” to ENTRY-FOUND ELSE ADD 1 to ROW-SUB IF ROW-SUB > 8 THEN MOVE “YES” to SEARCH-FINISHED MOVE “NO” to ENTRY-FOUND ENDLOOK-FOR-ENTRY What Happens When Values Are Part of a Range? What does it mean if ENTRY-FOUND is “NO”?

  41. Differences Among Programming Languages • Subscripts • First subscript may be 0 or 1 • Specify range of subscripts • Sales-Array subscripts ranging from 1980-2000 • Sales-Array(1999) • Most languages require that all data in an array contain the same type of data • An array cannot contain both numbers and nonnumbers • Use of corresponding arrays

  42. Name-Array Number-And-Code-Array Corresponding Arrays: Suppose you want to find the code and name for number 44563 • 2 • 1 • 3 • 30987 4 • 44563 5 • 48723 1 • 56560 3 • 57341 3 • 59992 2 • 60132 4 Ronald Gupta Sonal Desai Miriam Abrams Carol Burns Eric Johnson Peter Legardo Alison Gaynor Diana Chin Michael Cernowitz Marla Kaplan

  43. Number-And-Code-Array(5,1) contains 44563 Number-And-Code-Array Name-Array • 2 • 1 • 3 • 30987 4 • 44563 5 • 48723 1 • 56560 3 • 57341 3 • 59992 2 • 60132 4 Ronald Gupta Sonal Desai Miriam Abrams Carol Burns Eric Johnson Peter Legardo Alison Gaynor Diana Chin Michael Cernowitz Marla Kaplan

  44. Number-And-Code-Array (5,2) Name-Array (5) Number-And-Code-Array(5,1) contains 44563 Where is the Code? Where is the Name? Name-Array Number-And-Code-Array • 2 • 1 • 3 • 30987 4 • 44563 5 • 48723 1 • 56560 3 • 57341 3 • 59992 2 • 60132 4 Ronald Gupta Sonal Desai Miriam Abrams Carol Burns Eric Johnson Peter Legardo Alison Gaynor Diana Chin Michael Cernowitz Marla Kaplan

  45. Suppose You Want to Find the Code and Name for Number 44563 • Number-And-Code-Array(5,1) contains 44563 • The code is found in Number-And-Code-Array(5,2) • The name is found in Name-Array(5) • Notice that the row subscripts are all the same Number-And-Code-Array Name-Array • 2 • 1 • 3 • 30987 4 • 44563 5 • 48723 1 • 56560 3 • 57341 3 • 59992 2 • 60132 4 Ronald Gupta Sonal Desai Miriam Abrams Carol Burns Eric Johnson Peter Legardo Alison Gaynor Diana Chin Michael Cernowitz Marla Kaplan

  46. Corresponding Arrays (also called Parallel Arrays) have row subscripts that are the same in each of the arrays, that is, they correspond

More Related