370 likes | 862 Views
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
E N D
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 • Declare and initialize a two-dimensional array • Enter data into a two-dimensional array Introduction to Programming with C++, Fourth Edition
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
One-Dimensional Arrays • Each variable in a one-dimensional array is identified by a unique number called a subscript • Thesubscript 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
Names of the Variables in a One-Dimensional Array Named prices Introduction to Programming with C++, Fourth Edition
Declaring and Initializing One-Dimensional Arrays Introduction to Programming with C++, Fourth Edition
The letters Array in Memory Introduction to Programming with C++, Fourth Edition
Storing Data in a One-Dimensional Array Introduction to Programming with C++, Fourth Edition
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
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
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
displayMonths() Function Introduction to Programming with C++, Fourth Edition
Using the Subscript to Access an Element in a One-Dimensional Array Introduction to Programming with C++, Fourth Edition
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
Searching a One-Dimensional Array (continued) Introduction to Programming with C++, Fourth Edition
Calculating the Average Amount Stored in a One-Dimensional Numeric Array Introduction to Programming with C++, Fourth Edition
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
Determining the Highest Value Stored in a One-Dimensional Array (continued) Introduction to Programming with C++, Fourth Edition
Updating the Values Stored in a One-Dimensional Array Introduction to Programming with C++, Fourth Edition
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
Bubble Sort Introduction to Programming with C++, Fourth Edition
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
Passing a One-Dimensional Array to a Function (continued) Introduction to Programming with C++, Fourth Edition
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
Using Parallel One-Dimensional Arrays (continued) Introduction to Programming with C++, Fourth Edition
Using Parallel One-Dimensional Arrays (continued) Introduction to Programming with C++, Fourth Edition
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
Two-Dimensional Arrays (continued) Introduction to Programming with C++, Fourth Edition
Two-Dimensional Arrays (continued) Introduction to Programming with C++, Fourth Edition
Two-Dimensional Array in Memory Introduction to Programming with C++, Fourth Edition
Storing Data in a Two-Dimensional Array Introduction to Programming with C++, Fourth Edition
Storing Data in a Two-Dimensional Array (continued) Introduction to Programming with C++, Fourth Edition
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 shouldinitialize them Introduction to Programming with C++, Fourth Edition
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
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 Introduction to Programming with C++, Fourth Edition