an object-oriented approach to programming logic and design chapter 7 arrays
TRANSCRIPT
An Object-Oriented Approach to An Object-Oriented Approach to
Programming Logic and DesignProgramming Logic and Design
Chapter 7Arrays
An Object-Oriented Approach to Programming Logic and Design 2
ObjectivesObjectives
• Understand arrays and how they occupy computer memory
• Manipulate an array to replace nested decisions
• Declare and initialize an array• Understand the difference between initialized
and uninitialized arrays• Load array values from a file
An Object-Oriented Approach to Programming Logic and Design 3
Objectives (continued)Objectives (continued)
• Search an array for an exact match• Use a constant to store an array size• Search an array for a range match
An Object-Oriented Approach to Programming Logic and Design 4
Understanding Arrays Understanding Arrays and How They Occupy Memoryand How They Occupy Memory
• Array: a series or list of variables in memory that have the same name and type but different subscripts (or indexes)
• Subscript (or index): a number that indicates the position of an item within the array
Example: mailboxes in an apartment building – all have the same street address (name), but each has a different apartment number (subscript)
An Object-Oriented Approach to Programming Logic and Design 5
How Arrays Occupy How Arrays Occupy Computer MemoryComputer Memory
• Element: one item in the array; has a common name and a unique subscript, and stores a value
• Size of the array: the number of elements in the array
• Subscripts start at 0 • An array’s elements are stored contiguously in
memory
An Object-Oriented Approach to Programming Logic and Design 6
How Arrays Occupy How Arrays Occupy Computer Memory (continued)Computer Memory (continued)
An Object-Oriented Approach to Programming Logic and Design 7
Manipulating an Array to Replace Manipulating an Array to Replace Nested DecisionsNested Decisions
An Object-Oriented Approach to Programming Logic and Design 8
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
An Object-Oriented Approach to Programming Logic and Design 9
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
• Using an array can greatly reduce the amount of code required
• Subscript can be represented by a constant or a variable
• Using a variable for the subscript allows the use of a loop to cycle through all elements in an array
An Object-Oriented Approach to Programming Logic and Design 10
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
An Object-Oriented Approach to Programming Logic and Design 11
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
An Object-Oriented Approach to Programming Logic and Design 12
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
• Because the action taken was the same for each element of the array, the code can be significantly shortened
• Using a variable for the subscript replaces the need for decision structures in this example
An Object-Oriented Approach to Programming Logic and Design 13
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
An Object-Oriented Approach to Programming Logic and Design 14
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
An Object-Oriented Approach to Programming Logic and Design 15
Manipulating an Array to Replace Manipulating an Array to Replace Nested Decisions (continued)Nested Decisions (continued)
• Subscript – Can have any legal name
– Must be numeric with no decimal places
– Must be initialized to 0
– Must be incremented by 1 each time the logic passes through a loop
• Arrays make code easier to read
An Object-Oriented Approach to Programming Logic and Design 16
Array Declaration and InitializationArray Declaration and Initialization
• Declaring an array requires the type, the name, and the number of elements
Example: numeric count[20]• Individual array elements must be initialized• If elements will contain different values, initialize
each element separately• Some languages allow declaration and initialization
in a single statement
Example: numeric count[0] = 5
An Object-Oriented Approach to Programming Logic and Design 17
Array Declaration and Initialization Array Declaration and Initialization (continued(continued))
• If all elements in an array will have the same initial value, use a loop
• Providing values for an array is called populating the array
An Object-Oriented Approach to Programming Logic and Design 18
Initialized and Uninitialized ArraysInitialized and Uninitialized Arrays
• Uninitialized array: does not receive values for the elements until run time
• Initialized array: values for the elements are “hard-coded” by the programmer
• An array can be thought of as a table
An Object-Oriented Approach to Programming Logic and Design 19
Initialized and Uninitialized Arrays Initialized and Uninitialized Arrays (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 20
Loading an Array from a FileLoading an Array from a File
• Hard-coded values are inconvenient – only the programmer can change them
• Initializing an array by reading values from a file makes the program more flexible
An Object-Oriented Approach to Programming Logic and Design 21
Loading an Array from a File Loading an Array from a File (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 22
Searching an Array for an Exact MatchSearching an Array for an Exact Match
• Use a loop to search through an array, element by element
• The loop control variable can be used as the array subscript variable
• Flag: a variable used to indicate if an event has occurred
• Start with flag = “No”; set it to “Yes” if you find the desired value in an array element
An Object-Oriented Approach to Programming Logic and Design 23
Searching an Array for an Exact Match Searching an Array for an Exact Match (continued)(continued)
An Object-Oriented Approach to Programming Logic and Design 24
Using Parallel ArraysUsing Parallel Arrays
• Parallel arrays: two arrays in which each element in one array is associated with the element in the same relative position in the other array
Example: one array holds product numbers, the other holds the prices for each product
An Object-Oriented Approach to Programming Logic and Design 25
Using Parallel Arrays (continued)Using Parallel Arrays (continued)
An Object-Oriented Approach to Programming Logic and Design 26
Improving Search Efficiency Improving Search Efficiency Using an Early ExitUsing an Early Exit
• When using a loop to search an array, exit the loop if the item is found in an array element
• Early Exit: leaving a loop when a match is found
An Object-Oriented Approach to Programming Logic and Design 27
Improving Search Efficiency Improving Search Efficiency Using an Early Exit (continued)Using an Early Exit (continued)
An Object-Oriented Approach to Programming Logic and Design 28
Using a Constant Using a Constant to Store an Array Sizeto Store an Array Size
• Instead of hard-coding the array size, use a named constant or variable
• Advantage: only the variable or constant needs to be changed to change the array size
• Named constant identifiers should be written in all caps
Example: NUM_PRODUCTS
An Object-Oriented Approach to Programming Logic and Design 29
Using a Constant Using a Constant to Store an Array Size (continued)to Store an Array Size (continued)
An Object-Oriented Approach to Programming Logic and Design 30
Searching an Array for a Range MatchSearching an Array for a Range Match
• Range match: finding a value within a contiguous range of values
Example: range is 0 through 8
An Object-Oriented Approach to Programming Logic and Design 31
Searching an Array Searching an Array for a Range Match (continued)for a Range Match (continued)
An Object-Oriented Approach to Programming Logic and Design 32
Searching an Array Searching an Array for a Range Match (continued)for a Range Match (continued)
An Object-Oriented Approach to Programming Logic and Design 33
SummarySummary
• Array: a series of variables in memory which all have the same name but different subscripts
• Each array element has a unique subscript to identify it from other array elements
• Subscript (or index): a number indicating the position of the element within the array
• Use a variable for the subscript to allow more programmatic control of the array
An Object-Oriented Approach to Programming Logic and Design 34
Summary (continued)Summary (continued)
• Array element values can be hard-coded, or assigned at run time
• Array element values can be loaded from a file• Searching an array involves examining each
element– Loop through all elements– Use a flag to indicate when the matching value
is found• Flag: a variable that indicates if an event has
occurred
An Object-Oriented Approach to Programming Logic and Design 35
Summary (continued)Summary (continued)
• Parallel arrays are two arrays in which each element of one array is related to the element in the same subscript position in the other array
• When comparing a range of values, use either the low-end or high-end values of each range