Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.

Similar presentations


Presentation on theme: "Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array."— Presentation transcript:

1 Computer Programming TCP1224 Chapter 11 Arrays

2 Objectives Using Arrays 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 Search a two-dimensional array 2

3 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 3

4 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 4 Element

5 5

6 Storing Data in a One-Dimensional Array Can use an assignment statement to enter data into an array ▫See Next Slide Can use >> and getline() ▫See Slide 8 6

7 7

8 Storing Data in a One-Dimensional Array (continued) 8

9 Manipulating One-Dimensional Arrays Common array manipulation functions ▫Display the contents of an array ▫Access an array element using its subscript ▫Search an array (Lab 9) ▫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 (Lab 9) 9

10 Displaying the Contents of a One-Dimensional Array Function may need simply to display the contents of an array 10

11 Using the Subscript to Access an Element in a 1-D Array Before accessing element, verify if subscript is valid 11

12 Calculating the Average Amount Stored in a 1-D Numeric Array Prof. Jeremiah wants to calculate and display average test score earned by students on final ▫Function adds test scores that are stored in array  Then, divide sum by number of elements 12

13 Calculating the Average Amount Stored in a 1-D Numeric Array 13

14 Determining the Highest Value Stored in a One-Dimensional Array Sharon Johnson keeps track of the amount of money she earns each week ▫Function displays highest amount earned in a week  Stores pay amounts in a one-dimensional array 14

15 15

16 Updating the Values Stored in a 1-D Array Sales manager at Jillian Company wants a function that increases the price of each item company sells ▫Function stores prices in a one-dimensional array  Then, increase value stored in each element  Also, display each item’s new price on screen 16

17 17

18 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 18

19 19

20 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 20

21 Using Parallel 1-D Arrays Takoda Tapahe wants a program that displays the price of the item whose product ID she enters 21

22 Using Parallel 1-D Arrays Parallel arrays are two or more arrays whose elements are related by their position in the arrays ▫Related by their subscript 22

23 23

24 24

25 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 25

26 26

27 Two-Dimensional Arrays 27

28 Storing Data in a 2-D 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 28

29 29

30 Storing Data in a 2-D Array 30

31 31

32 Searching a Two-Dimensional Array Two one-dimensional (parallel) arrays can be replaced with one two-dimensional array 32

33 33

34 34

35 Searching a 2-D Array 35

36 Summary 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 36


Download ppt "Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array."

Similar presentations


Ads by Google