Programming Logic and Design Sixth Edition
Programming Logic and Design Sixth Edition
Chapter 6 Arrays
Objectives
In this chapter, you will learn about: Arrays and how they occupy computer memory Manipulating an array to replace nested decisions Using constants with arrays Searching an array Using parallel arrays
Objectives (continued)
Searching an array for a range match Remaining within array bounds Using a for loop to process arrays
Figure 6-3 Flowchart and pseudocode of decision-making process using a series of decisionsthe hard way
Programming Logic & Design, Sixth Edition 9
Figure 6-4 Flowchart and pseudocode of decision-making processbut still the hard way
Programming Logic & Design, Sixth Edition 11
Figure 6-5 Flowchart and pseudocode of decision-making process using an arraybut still a hard way
Programming Logic & Design, Sixth Edition 12
Figure 6-6 Flowchart and pseudocode of efficient decision-making process using an array
Programming Logic & Design, Sixth Edition 13
Figure 6-7 Flowchart and pseudocode for Dependents Report program (continued)
Programming Logic & Design, Sixth Edition 15
16
17
string MONTH[12] = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October, "November", "December"
18
19
Searching an Array
Sometimes must search through an array to find a value Example: mail-order business
Item numbers are three-digit, non-consecutive numbers Customer orders an item, check if item number is valid Create an array that holds valid item numbers Search array for exact match
20
Figure 6-8 Flowchart and pseudocode for program that verifies item availability
Programming Logic & Design, Sixth Edition 21
Figure 6-8 Flowchart and pseudocode for program that verifies item availability (continued)
Programming Logic & Design, Sixth Edition 22
Figure 6-8 Flowchart and pseudocode for program that verifies item availability (continued)
Programming Logic & Design, Sixth Edition 23
Each price in valid item price array in same position as corresponding item in valid item number array
Parallel arrays
Each element in one array associated with element in same relative position in other array
26
27
Figure 6-10 Flowchart and pseudocode of program that finds an items price using parallel arrays
Programming Logic & Design, Sixth Edition 28
Figure 6-10 Flowchart and pseudocode of program that finds an items price using parallel arrays (continued)
Programming Logic & Design, Sixth Edition 29
Figure 6-10 Flowchart and pseudocode of program that finds an items price using parallel arrays (continued)
Programming Logic & Design, Sixth Edition 30
31
Figure 6-11 Flowchart and pseudocode of the module that finds item price, exiting the loop as soon as it is found
Programming Logic & Design, Sixth Edition 32
Figure 6-11 Flowchart and pseudocode of the module that finds item price, exiting the loop as soon as it is found (continued)
Programming Logic & Design, Sixth Edition 33
First approach
Array with as many elements as each possible order quantity Store appropriate discount for each possible order quantity
Programming Logic & Design, Sixth Edition 34
35
Better approach
Create four discount array elements for each discount rate Parallel array with discount range
37
Arrays composed of elements of same data type Elements of same data type occupy same number of bytes in memory Number of bytes in an array is always a multiple of number of array elements Access data using subscript containing a value that accesses memory occupied by the array
Programming Logic & Design, Sixth Edition 39
Figure 6-16 Determining the month string from users numeric entry
Programming Logic & Design, Sixth Edition 40
Invalid array subscript is a logical error Out of bounds: using a subscript that is not within the acceptable range for the array Program should prevent bounds errors
Programming Logic & Design, Sixth Edition 41
Must stay within array bounds Highest usable subscript is one less than array size
Programming Logic & Design, Sixth Edition 42
Figure 6-17 Pseudocode that uses a for loop to display an array of department names
43
Figure 6-26 Pseudocode that uses a more efficient for loop to output month names
44
Summary
Array: series or list of variables in memory
Same name and type Different subscript
Use a variable as a subscript to the array to replace multiple nested decisions Some array values determined during program execution
Other arrays have hard-coded values
45
Summary (continued)
Search an array
Initialize the subscript Test each array element value in a loop Set a flag when a match is found
Parallel arrays: each element in one array is associated with the element in second array
Elements have same relative position
For range comparisons, store either the low- or high-end value of each range
46
Summary (continued)
Access data in an array
Use subscript containing a value that accesses memory occupied by the array
Subscript is out of bounds if not within defined range of acceptable subscripts for loop is a convenient tool for working with arrays
Process each element of an array from beginning to end
47