Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.

Similar presentations


Presentation on theme: "An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays."— Presentation transcript:

1 An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays

2 An Introduction to Programming with C++, Fifth Edition2 Objectives Declare and initialize a one-dimensional array Manipulate a one-dimensional array Explain the bubble sort algorithm Pass a one-dimensional array to a function Use parallel one-dimensional arrays

3 An Introduction to Programming with C++, Fifth Edition3 Objectives (continued) Declare and initialize a two-dimensional array Enter data into a two-dimensional array Search a two-dimensional array Utilize a one-dimensional array in a.NET C++ program

4 An Introduction to Programming with C++, Fifth Edition4 Concept Lesson Using Arrays One-Dimensional Arrays Storing Data in a One-Dimensional Array Manipulating One-Dimensional Arrays Passing a One-Dimensional Array to a Function

5 An Introduction to Programming with C++, Fifth Edition5 Concept Lesson (continued) Using Parallel One-Dimensional Arrays Two-Dimensional Arrays Storing Data in a Two-Dimensional Array Searching a Two-Dimensional Array

6 An Introduction to Programming with C++, Fifth Edition6 Using Arrays Simple variable is unrelated to other variables in computer’s internal memory –Also called a scalar variable Array is a group of variables –Same name and data type –Variables are related in some way –Most commonly used One-dimensional Three-dimensional

7 An Introduction to Programming with C++, Fifth Edition7 One-Dimensional Arrays Visualize a one-dimensional array as a column of variables –Subscript (or index) identifies each variable Indicates the variable’s position in the array Subscript of first variable is 0 Begin by populating the array so it does not contain garbage Element

8 An Introduction to Programming with C++, Fifth Edition8 One-Dimensional Arrays (continued)

9 An Introduction to Programming with C++, Fifth Edition9 Manipulating One-Dimensional Arrays Common array manipulation functions –Display the contents of an array –Access an array element using its subscript –Search an array –Calculate the average of the data stored in an array –Find the highest value stored in an array –Update the array elements –Sort the array elements using bubble sort

10 An Introduction to Programming with C++, Fifth Edition10 Displaying the Contents of a One-Dimensional Array Function may need simply to display the contents of an array

11 An Introduction to Programming with C++, Fifth Edition11 Displaying the Contents of a One-Dimensional Array (continued)

12 An Introduction to Programming with C++, Fifth Edition12 Using the Subscript to Access an Element in a One-Dimensional Array Before accessing element, verify if subscript is valid

13 An Introduction to Programming with C++, Fifth Edition13 Sorting the Values Stored in a One-Dimensional Array Arranging data in a specific order is called sorting –Ascending or descending order Bubble sort is quick and easy (for small arrays) –Adjacent elements that are out of order are swapped

14 An Introduction to Programming with C++, Fifth Edition14 Passing a One-Dimensional Array to a Function By default, scalar variables are passed by value –To pass by reference, use address-of (&) operator Arrays in C++ are passed by reference –More efficient –Address of first element is passed to function –Address-of operator is not used

15 An Introduction to Programming with C++, Fifth Edition15 Passing a One-Dimensional Array to a Function (continued)

16 An Introduction to Programming with C++, Fifth Edition16 Using Parallel One-Dimensional Arrays (continued) Parallel arrays are two or more arrays whose elements are related by their position in the arrays –Related by their subscript

17 An Introduction to Programming with C++, Fifth Edition17 Two-Dimensional Arrays Two-dimensional array resembles a table –Elements are in rows and columns –Each element is identified by two subscripts Subscripts specify the variable’s row and column position in the array

18 An Introduction to Programming with C++, Fifth Edition18 Two-Dimensional Arrays (continued)

19 An Introduction to Programming with C++, Fifth Edition19 Two-Dimensional Arrays (continued)

20 An Introduction to Programming with C++, Fifth Edition20 Storing Data in a Two-Dimensional Array You can use an assignment statement to enter data into a two-dimensional array –Or use >> and getline() Use two loops to access every element in a two- dimensional array –One loop tracks row subscript –Other loop tracks column subscript

21 An Introduction to Programming with C++, Fifth Edition21 Searching a Two-Dimensional Array Two one-dimensional (parallel) arrays can be replaced with one two-dimensional array

22 An Introduction to Programming with C++, Fifth Edition22 Summary An array is a group of variables that have the same name and data type and are related in some way –One-dimensional Visualize as a column of variables –Two-dimensional Visualize as a table You must declare an array before you can use it Each elements in a one-dimensional array is assigned a subscript –First element has subscript 0

23 An Introduction to Programming with C++, Fifth Edition23 Summary (continued) Parallel arrays are two or more arrays whose elements are related by their subscript in the arrays To create a two-dimensional array specify the number of rows and columns –Each element is identified by two subscripts First subscript represents the element’s row location Second subscript represents its column location Use two loops to access every element in a two- dimensional array

24 An Introduction to Programming with C++, Fifth Edition24 Application Lesson: Using Arrays in a C++ Program Lab 11.1: Stop and Analyze Lab 11.2 –Program should either display the monthly rainfall amounts on the screen or calculate and display the total rainfall amount Lab 11.3 –Modified program will use a value-returning function, calcTotal(), to calculate the total rainfall amount Lab 11.4: Desk-Check Lab Lab 11.5: Debugging Lab


Download ppt "An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays."

Similar presentations


Ads by Google