Programming Logic and Design: Eighth Edition
Programming Logic and Design: Eighth Edition
Eighth Edition
Chapter 6
Arrays
Objectives
In this chapter, you will learn about:
• Arrays
• How an array can replace nested decisions
• Using constants with arrays
• Searching an array for an exact match
• Using parallel arrays
• Searching an array for a range match
• Using a for loop to process arrays
When programmers
refer to array
element
prices[0], they
say “prices sub 0”
or simply “prices
zero.”
Figure 6-8 Flowchart and pseudocode for a program that verifies item availability (Continues)
Figure 6-8 Flowchart and pseudocode for a program that verifies item availability (Continues)
Figure 6-10 Flowchart and pseudocode of a program that finds an item price using
parallel arrays (Continues)
Programming Logic and Design, Eighth Edition 26
Using
Parallel
Arrays
(continued)
Figure 6-10 Flowchart and pseudocode of a program that finds an item price using
parallel arrays (Continues)
Programming Logic and Design, Eighth Edition 27
Using
Parallel
Arrays
(continued)
Figure 6-11 Flowchart and pseudocode of the module that finds an item price and
exits the loop as soon as it is found (Continues)
Figure 6-11 Flowchart and pseudocode of the module that finds an item price and
exits the loop as soon as it is found
Figure 6-17 determining the month string from a user’s numeric entry
Programming Logic and Design, Eighth Edition 40
Using a for Loop to Process Arrays
• for loop: a single statement
– Initializes the loop control variable
– Compares it to a limit
– Alters it
• The for loop is especially convenient when
working with arrays
– To process every element
• Must stay within array bounds
• Highest usable subscript is one less than the array
size
Programming Logic and Design, Eighth Edition 41
Using a for Loop to Process
Arrays (continued)
Figure 6-17 Pseudocode that uses a for loop to display an array of department names
Figure 6-19 Pseudocode that uses a more efficient for loop to output department names