C++ For Engineers and Scientists 4th Edition Bronson Solutions Manual Download
C++ For Engineers and Scientists 4th Edition Bronson Solutions Manual Download
Chapter 7
Arrays
At a Glance
• Objectives
• Teaching Tips
• Quick Quizzes
• Additional Projects
• Additional Resources
C++ for Engineers and Scientists, Fourth Edition 7-2
• Key Terms
C++ for Engineers and Scientists, Fourth Edition 7-3
Lecture Notes
Overview
All the variables your students have used so far have a common characteristic: Each variable can
be used to store only a single value at a time. When variables are of different data types, each
variable can store only one value of the declared data type. These types of variables are called
atomic variables (also referred to as scalar variables), which means their values cannot be further
subdivided or separated into a legitimate data type.
Often your students have a set of values, all of the same data type, that form a logical group. A
simple list containing items of the same data type is called a one-dimensional array. This chapter
describes how one-dimensional arrays are declared, initialized, stored in a computer, and used.
Your students will also explore the use of one-dimensional arrays with sample programs and see
the procedures for declaring and using multidimensional arrays.
Objectives
In this chapter, students will learn about:
• One-dimensional arrays
• Array initialization
• Declaring and processing two-dimensional arrays
• Arrays as arguments
• A case study about statistical analysis
• The Standard Template Library (STL)
• Searching and sorting
• Common programming errors
Teaching Tips
7.1 One-Dimensional Arrays
Teaching
Help students visualize arrays by working through the examples in detail.
Tip
Teaching
Students will need to be reminded often that array indexes are zero-relative.
Tip
5. Describe how an index represents the offset from the start of the array.
6. Introduce the term “subscripted variable” as another name for an element, and point out
that it can be used anywhere that a normal variable can be used.
Teaching Students often have trouble grasping the abstraction of expressions for indexes.
Tip Actual examples with expressions for indexes will be helpful.
8. Describe the use of loops for cycling through all of the elements in an array. Explain how
the loop counter is used as the array index.
Teaching
Tip Cycling through an array using a loop is like assembly-line processing.
9. Point out that invalid array indexes are not detected at compile time, and may or may not
be immediately detected at run time.
Teaching If an invalid index value points to a valid memory location, unexpected results
Tip may occur, including the overwriting of that memory location. These kinds of
errors are difficult to find. Good “desk-checking” of code is recommended.
C++ for Engineers and Scientists, Fourth Edition 7-5
1. Point out that an array element can be assigned a value interactively by using a cin
statement.
Quick Quiz 1
1. What name is given to describe a single item within an array?
Answer: an array element
2. If the highest subscript in an array is 9, and the subscripts start at 0, how many elements
are in the array?
Answer: 10
3. When you declare an array, what will all of the array’s elements have in common?
Answer: the same name and the same type of data
4. When you declare an array, what will be unique for each array element?
Answer: the array element’s subscript
2. Point out that C++ ignores white space, allowing the initialization to span multiple lines.
3. Describe the additional character position at the end of a char array, containing a null
character.
Teaching
Tip Many languages use a null character to indicate the end of a string value.
Quick Quiz 2
1. True or False: An array can be initialized in the declaration statement.
Answer: True
Teaching Point out the convention of always specifying the row value first, and then the
Tip column value.
3. Describe the initialization of a two-dimensional array, using braces to separate the rows.
4. Discuss the use of nested for loops for processing two-dimensional arrays.
1. Point out that arrays of higher dimensions can be created, but are not commonly used.
1. Describe how an array element can be used as a function argument in the same fashion as
a simple variable.
Teaching Note that an array element is passed by value (a copy of the element’s value is
Tip passed to the function).
2. Describe how an entire array can be passed as an argument to a function that has declared
an array argument.
C++ for Engineers and Scientists, Fourth Edition 7-7
1. Describe the algorithm used to determine the address of each element in an array.
Quick Quiz 4
1. When declaring a two-dimensional array, which comes first in the size specifications,
rows or columns?
Answer: rows
2. True or False: When assigning values to elements in a two-dimensional array, values are
assigned in row order.
Answer: False
3. True or False: Array elements are passed to a called function in the same manner as
scalar variables.
Answer: True
1. Discuss both of the applications in this section in detail, using the four-step procedure.
Teaching Expect to spend a fair amount of time on these applications, as students are
Tip probably still having difficulty grasping the concept of arrays.
C++ for Engineers and Scientists, Fourth Edition 7-8
Teaching Stress the major difference between an array and a vector: A vector will
Tip automatically expand as needed.
5. Note that several of the STL algorithms are common operations that are available for the
data structures.
Teaching Remind students that the vector and algorithm preprocessor statements are
Tip required, along with the namespace std.
6. Describe the syntax for creating and initializing a vector, modifying an element, and
inserting a new element.
Quick Quiz 6
1. True or False: It is useful to use arrays when multiple passes through the same data set
are required.
Answer: True
1. Introduce the two common methods for searching (linear and binary).
Big O Notation
Sort Algorithms
Teaching
Tip Point out that a linear search is acceptable when the list is relatively small.
3. Describe the binary search, noting that it requires the list to already be in sorted order.
Working through the numerical examples in the text for the maximum number of
Teaching passes in a binary search will help students understand how Big O notation is
Tip used.
Point out to students that the maximum number of steps involved in an algorithm
Teaching can become critical when dealing with extremely large sets of data, such as
Tip several million records.
9. Introduce the exchange, or bubble, sort and describe the steps involved.
C++ for Engineers and Scientists, Fourth Edition 7-10
10. Discuss the advantages and disadvantages of the bubble sort in comparison to the
selection sort.
Quick Quiz 7
1. True or False: A linear search examines every element in order until the desired item is
found.
Answer: True
2. True or False: A binary search will generally require examining more elements than a
linear search.
Answer: False. For data sets with more than fifty elements, a binary search is generally
more efficient.
Teaching Remind students that as they begin to use more complex structures in their
Tip programs, the debugging task may become more challenging.
Quick Quiz 8
1. True or False: An array index that references an element beyond the end of the array will
not cause a problem for a program.
Answer: False. Using a subscript that references a nonexistent array element is a common
programming error.
Additional Projects
1. Have the students write a small C++ program that declares and populates an array to hold
five student test scores. Have them write the code that would calculate the student’s
average for the test scores.
2. Have students write a program that takes an array of integers in random order and uses
binary search to find one of the elements. The program must use one of the sorting
functions to sort the array before calling binary search.
Additional Resources
1. Tutorial on arrays:
http://www.cplusplus.com/doc/tutorial/tut3-1.html
3. Animated comparison of sort algorithms (click on the display boxes to run the
algorithms):
http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
Key Terms
➢ Array: A list of related values of the same data type stored with the same name
➢ Array element: An individual item in an array
➢ Array name: The name associated with a group of elements
➢ Binary search: A process for searching a sorted list by dividing it in half with each
check for the desired element until the element is discovered or the list is exhausted
➢ Bounds check: The process of checking that the value of an array index remains valid
➢ Bubble sort: One of a group of sort algorithms that implement an exchange sort
➢ Collection: A synonym for container
➢ Component: A synonym for element
➢ Container: A template that permits constructing a generic data structure
➢ Element: An item in an array
C++ for Engineers and Scientists, Fourth Edition 7-12
➢ Exchange sort: A sort algorithm in which adjacent elements of the list are exchanged
with one another so that the list becomes sorted
➢ External sorts: A sort that is performed using external storage when the data list is too
large to be contained completely in memory
➢ Index: The integer-valued relative position of an array element; used as an offset from
the beginning of the array
➢ Indexed variable: A variable in an array referenced through an index value
➢ Internal sorts: A sort that is performed completely in memory when the data list is not
too large
➢ Linear search: A search in which each item in a list is examined in the order in which it
occurs until the desired item is found or the end of the list is reached
➢ List: A synonym for container
➢ Null character: The character \0
➢ One-dimensional array: A list array with a single row of data
➢ Selection sort: A sort algorithm in which the smallest value is selected from the
complete list and exchanged with the first element in the list
➢ Sequential search: A synonym for linear search
➢ Single-dimensional array: A synonym for one-dimensional array
➢ Standard Template Library: A useful set of tested and generic classes that can be
modified, expanded, and contracted
➢ Subscript: A synonym for index
➢ Subscripted variable: A synonym for indexed variable
➢ Two-dimensional array: A table of data with rows and columns
➢ Vector: A container class that functions similarly to an array, but will automatically
expand as needed