You are on page 1of 10

Programming Logic and Design, Ninth Edition 6-1

Chapter 6
Arrays
A Guide to this Instructor’s Manual:

We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.

This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings you will find: lecture notes that summarize the section, Teacher
Tips, Classroom Activities, and Lab Activities. Pay special attention to teaching tips and
activities geared towards quizzing your students and enhancing their critical thinking skills.

In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint Presentations, Test
Banks, and other supplements to aid in your teaching experience.

At a Glance

Instructor’s Manual Table of Contents

 Overview

 Chapter Objectives

 Teaching Tips

 Quick Quizzes

 Class Discussion Topics

 Additional Projects

 Additional Resources

 Key Terms

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-2

Lecture Notes

Overview

Chapter 6 provides an introduction to arrays and subscripts. Students will learn the basic
steps involved in declaring, initializing, loading, and searching arrays. They will learn
about using parallel arrays to solve problems. Students will become familiar with using
flags to indicate success in searching and to trigger early exits from search loops. Finally,
students will learn how to search for exact matches and for values within ranges.

Chapter Objectives

In this chapter, your students 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
 Remaining within array bounds
 Using a for loop to process arrays

Teaching Tips
Understanding Arrays

1. Introduce the concept of an array.

As an introduction, compare an array to one row in an Excel spreadsheet. Each


Teaching
cell has a unique identifier (the Column/Row address), and each cell contains a
Tip
value.

How Arrays Occupy Computer Memory

1. Stress the fact that all array items have the same name and the same data type, but are
uniquely identified by their index position.

2. Explain that data item is one element of an array


© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-3

3. Point out that the array elements (data items) are stored in contiguous locations in
memory, illustrated in Figure 6-1.

4. Point out that the size of the array depends on the number of data items it will hold.

5. Introduce the concept of a subscript (or index), which would correspond to the Excel
spreadsheet column number.

6. Explain that assigning values to the array elements is called populating the array.

A good conceptual example for arrays is to describe the addresses of several


Teaching people who all live in the same apartment building. Their addresses all have the
Tip same street name, but they have different apartment numbers. Relate the row of
mailboxes to an array, as holding places for (mail) values.

Another way to demonstrate arrays is to use an empty egg carton and label the
Teaching
egg slots with subscripts. Then place a set of small, similar objects in the egg
Tip
carton, one in each slot.

7. Describe how a subscript is denoted in programming languages.

8. Introduce the concept of zero-based counting for array subscripts.

Ask students to count (out loud) to the number 5. Very few of them (if any) will
Teaching
begin counting with 0. Use this as an example of how we must think like the
Tip
computer in order to process arrays.

Teaching Point out that the highest subscript value will always be one less than the size of
Tip the array (the number of elements) because subscripts are zero-relative.

How an Array Can Replace Nested Decisions

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-4

1. Introduce the example program and walk through Figure 6-3, noting that this approach
does not use arrays.

2. Using the flowcharts and pseudocode in Figures 6-4 and 6-5, explain that arrays can be
used in a program to replace a long series of decisions.

3. Using Figure 6-6, explain how to use an array to simplify the processing. The entire
program is shown in Figure 6-7.

Quick Quiz 1

1. Each element of an array has the same ____ and the same ____.
Answer: name, data type

2. Each element of an array has a unique ____.


Answer: subscript or index

3. (True/False) Array elements are stored sequentially in computer memory.


Answer: True

Using Constants with Arrays

1. Explain that there are three ways to use constants with arrays: to hold the size of an
array, as the array values, and as subscripts.

Using a Constant as the Size of an Array

1. Discuss the benefits of using a constant for the array size rather than a magic number.

Using Constants as Array Element Values

1. Review the example of using constants like month names in a MONTHS array for array
element values.

Using a Constant as an Array Subscript

1. Review the example in the text of using a constant as an array subscript.

Searching an Array for an Exact Match

1. Describe the use of the array to hold nonconsecutive item numbers, and introduce the
concept of searching for an item in an array using a linear search.

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-5

Teaching Remind students that using a loop with an array can replace a series of nested if
Tip statements.

2. Introduce the concept of a flag.

3. Describe the steps involved in searching an array, and how a flag can be used to
indicate the item has been found.

4. Review the sample program using Figure 6-8 and focusing on the array search portion.
Point out the use of the if statement nested within the while loop.

Using Parallel Arrays

1. Introduce the concept of parallel arrays, using Figure 6-9 to show how parallel arrays
appear in memory.

2. Explain that parallel arrays contain related data and the subscript relates the arrays. That
is, elements at the same position in each array are logically related.

This is a difficult concept for new programmers. Stress that the key to successful
parallel arrays is ensuring that related items are in the same position in their
Teaching
arrays. Then, the same subscript value used with each array will retrieve the
Tip
related items. A good example of this is to show the need to store a person’s first
name and last name separately—in parallel arrays.

3. Review the sample program logic using Figure 6-10. Point out how simple the code
becomes when using parallel arrays.

4. Explain that in Figure 6-10, the relationship between an item number and its price is an
indirect relationship.

Teaching Ask the students to consider how Figure 6-10 might be handled without using
Tip parallel arrays.

Improving Search Efficiency

1. Discuss the improvement in efficiency that can be realized by exiting a search loop
when a match is found.

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-6

2. Introduce the concept of exiting a loop early, and point out that its benefit increases
with the size of the array.

3. Explain the concept of a binary search.

Point out that as humans, we stop searching when we have found the desired
Teaching item. Our programs should do the same. Have the students search for a number
Tip between 1 and 100. Show how using a linear search will average 50 tests. Show
how using a binary search will use 6 tests.

4. Review the sample application using Figure 6-11 and noting the early exit code.

Searching an Array for a Range Match

1. Introduce the concept of searching for a match within a range of values.

Teaching Ask students to think about how this search might be implemented. Discuss why
Tip you would not want to test for every possible value within the range.

2. Review the sample application input using Figure 6-12, and walk through the
drawbacks of the initial approach listed in the text.

3. Discuss the solution of creating four discount array elements and using parallel arrays to
search for the appropriate discount level.

4. Emphasize that each array element holds a single value, not a range of values.

Teaching
This is a good time to revisit the concept of parallel arrays.
Tip

5. Point out that the array holds the low end of each quantity range, illustrated in Figure 6-
14.

6. Discuss why you should start with the last array element and work backward.

Teaching Ask the students what problems they would encounter if they started with the
Tip first array element and worked forward.

7. Review the sample program logic using Figure 6-15.


© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-7

Teaching Walk through the computation by hand with these two arrays to demonstrate to
Tip students how this logic works.

Remaining within Array Bounds

1. Remind students that arrays are composed of elements of the same data type, which
occupy the same number of bytes in memory, illustrated in Figure 6-16.

2. Explain that array subscripts must remain in bounds.

3. Walk through Figure 6-17, which uses an array to determine a month string from a
numeric entry. Discuss what happens if the user enters a value that is too small or too
large. Discuss possible ways to improve this program.

4. Point out that if the subscript number entered by the user is too small or too large, the
subscript is out of bounds.

Point out that different programming languages report this error differently. In
Teaching
addition to “subscript out of bounds” or “subscript out of range,” you may also
Tip
see messages such as “invalid index.”

Teaching Point out that array processing is prone to “off by one” errors because loops are
Tip used with zero-relative subscripts.

5. Point out that the program can check the validity of the data before using the user’s
input to index the array.

6. Review the use of zero-based arrays again, and review how to determine the array
bounds based on the size (number of elements) of the array.

Using a for Loop to Process an Array

1. Review the for loop, and point out that it is particularly convenient when working with
arrays.

2. Remind students that the highest usable subscript is one less than the size of the array.

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-8

For students new to programming, it may not be obvious that the last valid
Teaching
subscript is one less than the size of the array. Emphasize that if an array has 12
Tip
items, its valid subscripts are 0 through 11.

3. Walk through Figures 6-18 and 6-19 to illustrate how to use a for loop to output array
elements, and discuss why Figure 6-18 is less efficient than Figure 6-19.

Quick Quiz 2
1. What programming construct should be used to search through an array for a matching
item?
Answer: a loop

2. How are the items in two parallel arrays related?


Answer: by having the same relative position in the array

3. The first element in an array has a subscript of ____.


Answer: 0

4. The last element in an array has a subscript of ____.


Answer: one less than the size of the array

Class Discussion Topics

1. Consider the structure of a class roster for a university course. It contains a row for each
student enrolled in the course. What might the array used to produce this report look
like? Discuss the elements that would be needed in this structure.

2. At what point should you consider changing from individual variables to an array? Is it
reasonable to use an array when you only have two or three items to track? Why or why
not? Consider the number of sets of those two or three items when making your
decision.

3. What shortcomings are inherent in arrays because of the rule of storing the same data
type? How would a person’s contact information (name, address, city, state, zip, phone,
email) be stored? If it is in parallel arrays, is this efficient? This discussion should lead
to record structures.

4. A train schedule is an everyday, real-life example of an array. Identify at least four


more.

Student answers will vary but might include:

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-9

 tax table
 life expectancy table based on year of birth
 insurance company height and weight table
 loan amortization schedule
 company salary schedule
 instructor’s grading scale
 shipping charges based on distance
 sliding fees for a service based on income

5. Every element in an array always has the same data type. Why is this necessary?

Every element in an array has the same data type which forces every element in an array
to be the same size in bytes. Therefore, it becomes possible to use a subscript to evenly
measure an element's distance from the start of an array. For example, if you use a data
type that occupies exactly four bytes of memory then the array element at subscript 0 is
0 bytes away from the beginning of the array, the element at subscript 1 is 4 bytes away,
the element at subscript 2 is 8 bytes away, and so on.

Additional Projects

1. Write the pseudocode to do the following:


a. Read a data file containing student names, ID number, and gender into an array.
b. Print the contents of each record using the array.
c. Count the number of male and female students by using the array, and print the
counts.

2. Write the pseudocode to produce a bill for cell phone charges. Data fields will include
date, number called, number of minutes, and cost (rate × minutes). Data should be
loaded into an array. Include totals for the number of minutes and cost.

Additional Resources

1. JavaScript arrays:
http://www.devarticles.com/c/a/JavaScript/JavaScript-Arrays

2. Tutorial on arrays in C and C++:


http://www.augustcouncil.com/~tgibson/tutorial/arr.html

3. Tutorial on arrays in Java:


http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

4. Arrays in Visual Basic


https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 6-10

Key Terms

 Array – a series or list of values in computer memory, all of which have the same name
but are differentiated with special numbers called subscripts.
 Binary search – a search that starts in the middle of a sorted list and then determines
whether it should continue higher or lower to find a target value.
 Element – a single data item in an array.
 Flag – a variable that you set to indicate whether some event has occurred.
 In bounds – describes an array subscript that is within the range of acceptable
subscripts for its array
 Indirect relationship – describes the relationship between parallel arrays in which an
element in the first array does not directly access its corresponding value in the second
array.
 Linear search – a search through a list from one end to the other.
 Out of bounds – describes an array subscript that is not within the range of acceptable
subscripts for the array.
 Parallel arrays – each element in one array is associated with the element in the same
relative position in the other array(s).
 Populating the array – the act of assigning values to the array elements.
 Size of the array – the number of elements the array can hold.
 Subscript, also called an index – a number that indicates the position of a particular
item within an array.

© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

You might also like