35
Chapter 11: Arrays Introduction to Programming with C++ Fourth Edition

Chapter 11: Arrays

  • Upload
    garson

  • View
    26

  • Download
    0

Embed Size (px)

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

Page 1: Chapter 11: Arrays

Chapter 11:Arrays

Introduction to Programming with C++

Fourth Edition

Page 2: Chapter 11: Arrays

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

Page 3: Chapter 11: Arrays

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

Page 4: Chapter 11: Arrays

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

Page 5: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 5

Names of the Variables in a One-Dimensional Array Named prices

Page 6: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 6

Declaring and Initializing One-Dimensional Arrays

Page 7: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 7

The letters Array in Memory

Page 8: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 8

Storing Data in a One-Dimensional Array

Page 9: Chapter 11: Arrays

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

Page 10: Chapter 11: Arrays

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

Page 11: Chapter 11: Arrays

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

Page 12: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 12

displayMonths() Function

Page 13: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 13

Using the Subscript to Access an Element in a One-Dimensional Array

Page 14: Chapter 11: Arrays

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

Page 15: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 15

Searching a One-Dimensional Array (continued)

Page 16: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 16

Calculating the Average Amount Stored in a One-Dimensional Numeric Array

Page 17: Chapter 11: Arrays

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

Page 18: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 18

Determining the Highest Value Stored in a One-Dimensional Array (continued)

Page 19: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 19

Updating the Values Stored in a One-Dimensional Array

Page 20: Chapter 11: Arrays

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

Page 21: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 21

Bubble Sort

Page 22: Chapter 11: Arrays

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

Page 23: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 23

Passing a One-Dimensional Array to a Function (continued)

Page 24: Chapter 11: Arrays

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

Page 25: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 25

Using Parallel One-Dimensional Arrays (continued)

Page 26: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 26

Using Parallel One-Dimensional Arrays (continued)

Page 27: Chapter 11: Arrays

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

Page 28: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 28

Two-Dimensional Arrays (continued)

Page 29: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 29

Two-Dimensional Arrays (continued)

Page 30: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 30

Two-Dimensional Array in Memory

Page 31: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 31

Storing Data in a Two-Dimensional Array

Page 32: Chapter 11: Arrays

Introduction to Programming with C++, Fourth Edition 32

Storing Data in a Two-Dimensional Array (continued)

Page 33: Chapter 11: Arrays

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

Page 34: Chapter 11: Arrays

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

Page 35: Chapter 11: Arrays

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