View
27
Download
0
Category
Tags:
Preview:
DESCRIPTION
Chapter 11: Arrays. Introduction to Programming with C++ Fourth Edition. Objectives. Declare and initialize a one-dimensional array Manipulate a one-dimensional array Pass a one-dimensional array to a function Use parallel one-dimensional arrays - PowerPoint PPT Presentation
Citation preview
Chapter 11:Arrays
Introduction to Programming with C++
Fourth Edition
Introduction to Programming with C++, Fourth Edition 2
Objectives
• Declare and initialize a one-dimensional array• Manipulate a one-dimensional array• Pass a one-dimensional array to a function• Use parallel one-dimensional arrays• Declare and initialize a two-dimensional array• Enter data into a two-dimensional array
Introduction to Programming with C++, Fourth Edition 3
Using Arrays
• Simple or scalar variable - one that is unrelated to any other variable in memory
• Array - a group of variables that have the same name and data type and are related in some way
Introduction to Programming with C++, Fourth Edition 4
One-Dimensional Arrays
• Each variable in a one-dimensional array is identified by a unique number called a subscript
• The subscript indicates the variable’s position in the array
• First variable in a one-dimensional array is assigned a subscript of 0 (zero), the second a subscript of 1 (one), and so on
• Elements – array variables
Introduction to Programming with C++, Fourth Edition 5
Names of the Variables in a One-Dimensional Array Named prices
Introduction to Programming with C++, Fourth Edition 6
Declaring and Initializing One-Dimensional Arrays
Introduction to Programming with C++, Fourth Edition 7
The letters Array in Memory
Introduction to Programming with C++, Fourth Edition 8
Storing Data in a One-Dimensional Array
Introduction to Programming with C++, Fourth Edition 9
Manipulating One-Dimensional Arrays
• Display the contents of an array• Access an array element using its subscript• Search the array• Calculate the average of the data stored in a
numeric array
Introduction to Programming with C++, Fourth Edition 10
Manipulating One-Dimensional Arrays (continued)
• Find the highest value stored in an array• Update the array elements• Sort the array elements
Introduction to Programming with C++, Fourth Edition 11
Displaying the Contents of a One-Dimensional Array
• displayMonths() function – demonstrates how you can display the contents of the prices array
Introduction to Programming with C++, Fourth Edition 12
displayMonths() Function
Introduction to Programming with C++, Fourth Edition 13
Using the Subscript to Access an Element in a One-Dimensional Array
Introduction to Programming with C++, Fourth Edition 14
Searching a One-Dimensional Array
• Search through an array looking for elements that are greater than a particular value
Introduction to Programming with C++, Fourth Edition 15
Searching a One-Dimensional Array (continued)
Introduction to Programming with C++, Fourth Edition 16
Calculating the Average Amount Stored in a One-Dimensional Numeric Array
Introduction to Programming with C++, Fourth Edition 17
Determining the Highest Value Stored in a One-Dimensional Array
• Search through an array looking for an element whose value is larger than the largest value in the array so far (high)
• When the loop is finished, high will be the largest element in the array
Introduction to Programming with C++, Fourth Edition 18
Determining the Highest Value Stored in a One-Dimensional Array (continued)
Introduction to Programming with C++, Fourth Edition 19
Updating the Values Stored in a One-Dimensional Array
Introduction to Programming with C++, Fourth Edition 20
Sorting the Data Stored in a One-Dimensional Array
• Arranging data in a specific order is called sorting
• Bubble sort algorithm: compare adjacent array elements and interchange (swap) the ones that are out of order
Introduction to Programming with C++, Fourth Edition 21
Bubble Sort
Introduction to Programming with C++, Fourth Edition 22
Passing a One-Dimensional Array to a Function
• Arrays are passed by reference– Address of the first element is passed
• Include the name of the array in the function call• Do not include the address-of (&) operator
before the formal parameter’s name in the function header or prototype
• Formal parameter in the header/prototype should list data type, name, and empty square brackets
Introduction to Programming with C++, Fourth Edition 23
Passing a One-Dimensional Array to a Function (continued)
Introduction to Programming with C++, Fourth Edition 24
Using Parallel One-Dimensional Arrays
• Parallel arrays – two or more arrays whose elements are related by their position (subscript) in the arrays
Introduction to Programming with C++, Fourth Edition 25
Using Parallel One-Dimensional Arrays (continued)
Introduction to Programming with C++, Fourth Edition 26
Using Parallel One-Dimensional Arrays (continued)
Introduction to Programming with C++, Fourth Edition 27
Two-Dimensional Arrays
• Two-dimensional array resembles a table• Variables or elements are identified by a unique
combination of two subscripts• Subscripts specify the variable’s row and column
position in the array• Initialize elements by entering a separate
initialValues section, enclosed in braces, for each row in the array
Introduction to Programming with C++, Fourth Edition 28
Two-Dimensional Arrays (continued)
Introduction to Programming with C++, Fourth Edition 29
Two-Dimensional Arrays (continued)
Introduction to Programming with C++, Fourth Edition 30
Two-Dimensional Array in Memory
Introduction to Programming with C++, Fourth Edition 31
Storing Data in a Two-Dimensional Array
Introduction to Programming with C++, Fourth Edition 32
Storing Data in a Two-Dimensional Array (continued)
Introduction to Programming with C++, Fourth Edition 33
Summary
• Array - a group of variables that have the same name and data type
• Subscript – a unique number assigned to each array element in memory
• One-dimensional array - a column of variables• Two-dimensional array - resembles a table in
that it has rows and columns• You must declare all arrays and you should
initialize them
Introduction to Programming with C++, Fourth Edition 34
Summary (continued)
• Various array manipulations:– Displaying the contents of an array– Accessing an array element using its
subscript– Searching an array (“linear search”)– Calculating the average of the data– Finding the highest value stored in an array– Updating the array elements– Sorting the array elements
Introduction to Programming with C++, Fourth Edition 35
Summary (continued)
• Arrays are passed to functions by reference• Parallel arrays are two or more arrays whose
elements are related by their subscript (or position) in the arrays
Recommended