Programming with Microsoft Visual Basic 2012 Chapter 9

Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays

Previewing the Die Tracker Application Figure 9 -1 Result of clicking the Roll button the first time Programming with Microsoft Visual Basic 2012 2

Lesson A Objectives After studying Lesson A, you should be able to: • Declare and initialize a one-dimensional array • Store data in a one-dimensional array • Determine the number of array elements and the highest subscript • Traverse a one-dimensional array Programming with Microsoft Visual Basic 2012 3

Lesson A Objectives (cont. ) • Code a loop using the For Each…Next statement • Compute the total and average of a one-dimensional array’s contents • Find the highest value in a one-dimensional array • Sort a one-dimensional array Programming with Microsoft Visual Basic 2012 4

Arrays • Simple variable (also called a scalar variable) – One that is unrelated to any other variable in memory • Array – A group of related variables – Variables have the same name and data type • Reasons to use an array: – To simplify the process of coding an application – To increase the run-time efficiency of a program Programming with Microsoft Visual Basic 2012 5

One-Dimensional Arrays • One-dimensional array – Its variables are stored in consecutive memory locations – Visualized as a column of variables • Subscript – Indicates a variable’s position in the array – Starts at 0 for the first array variable • You refer to an array variable by the array name and subscript – Example: str. Cities(0) is the first variable in the str. Cities array Programming with Microsoft Visual Basic 2012 6

One-Dimensional Arrays (cont. ) Declaring a One-Dimensional Array • Use either {Dim | Private | Static} keywords • Depends on whether you are creating a procedure-level array or a class-level array • Array variables are referred to as elements • Arrays are initialized by the computer when they are created – Arrays of numeric variables are initialized to 0 – Arrays of string variables are initialized using the keyword Nothing – Arrays of Boolean variables are initialized using the Boolean keyword False – Arrays of Date variables are initialized to 12: 00 AM January 1, 0001 Programming with Microsoft Visual Basic 2012 7

One-Dimensional Arrays (cont. ) Declaring a One-Dimensional Array (cont. ) Figure 9 -3 Syntax versions and examples of declaring a one-dimensional array Programming with Microsoft Visual Basic 2012 8

One-Dimensional Arrays (cont. ) • Populating the array – Assigning initial values – After an array is declared, you can store data in the array – To enter data into an array: • Use an assignment statement • Use the Try. Parse statement – Example syntax using an assignment statement: arrayname(subscript) = value Figure 9 -4 Illustration of the str. States and dbl. Pays arrays Programming with Microsoft Visual Basic 2012 9

One-Dimensional Arrays Storing Data in a One-Dimensional Array • Assignment statements and statements that contain the Try. Parse method are used to store data in elements (cont. ) Figure 9 -5 Examples of statements used to store data in a one-dimensional array Programming with Microsoft Visual Basic 2012 10

One-Dimensional Arrays (cont. ) Determining the Number of Elements in a One-Dimensional Array • Length property – Stores the number of elements in an array • The number is an integer Figure 9 -6 Syntax and an example of a one-dimensional array’s Length property Programming with Microsoft Visual Basic 2012 11

One-Dimensional Arrays (cont. ) Determining the Highest Subscript in a One-Dimensional Array • To determine the highest subscript in a one-dimensional array, subtract 1 from the array’s Length property • Get. Upper. Bound method – Returns an integer that represents the highest subscript in the specified array dimension • For a one-dimensional array, the specified dimension is 0 Figure 9 -7 Syntax and an example of a one-dimensional array’s Get. Upper. Bound method Programming with Microsoft Visual Basic 2012 12

One-Dimensional Arrays (cont. ) Traversing a One-Dimensional Array • Traversing an array – Look at each array element, one by one • Beginning with the first element • Ending with the last element Figure 9 -8 Examples of loops used to traverse a onedimensional array Programming with Microsoft Visual Basic 2012 13

One-Dimensional Arrays (cont. ) Traversing a One-Dimensional Array (cont. ) Figure 9 -9 Sample run of the States application Programming with Microsoft Visual Basic 2012 14

The For Each…Next Statement • For Each…Next statement – Used to process each element in an array – Unlike the For…Next statement: • You do not have to keep track of array subscripts • It can only read array values, not permanently modify them • Declare a variable within the For Each…Next statement to refer to each array element, one at a time Figure 9 -10 Syntax and an example of the For Each…Next statement Programming with Microsoft Visual Basic 2012 15

Calculating the Total and Average Values • Brewers Coffee application – Displays the total number of pounds of coffee sold during a six-month period – Displays the average number of pounds sold each month • btn. Calc control’s Click event procedure – Adds array element values – Divides the total by the number of array elements – Displays the average amount on a form Figure 9 -11 Problem specification for the Brewers Coffee application Programming with Microsoft Visual Basic 2012 16

Calculating the Total and Average Values (cont. ) Figure 9 -13 Total and average amounts shown in the interface Figure 9 -12 Examples of accumulating the array values Programming with Microsoft Visual Basic 2012 17

Finding the Highest Value Figure 9 -14 Problem specification for the Car Emporium application Figure 9 -15 Pseudocode and flowchart for the btn. Get_Click procedure (continues) Programming with Microsoft Visual Basic 2012 18

Finding the Highest Value (cont. ) Figure 9 -16 Sample run of the Car Emporium application Figure 9 -15 Pseudocode and flowchart for the btn. Get_Click procedure Programming with Microsoft Visual Basic 2012 19

Finding the Highest Value (cont. ) Figure 9 -17 Get Highest button’s Click event procedure Programming with Microsoft Visual Basic 2012 20

Sorting a One-Dimensional Array • Sorting – Arranging data in a specific order • Array. Sort method – Sorts elements of a one-dimensional array in ascending order – Syntax: Array. Sort(array. Name) • To sort an array in descending order: – First use the Array. Sort method to sort in ascending order – Then use the Array. Reverse method to reverse the order of array elements Programming with Microsoft Visual Basic 2012 21

Sorting a One-Dimensional Array (cont. ) Figure 9 -18 Syntax and examples of the Array. Sort and Array. Reverse methods Programming with Microsoft Visual Basic 2012 22

Sorting a One-Dimensional Array (cont. ) Figure 9 -21 Continent names displayed in ascending order Figure 9 -20 Most of the Continent application’s code Programming with Microsoft Visual Basic 2012 23

Lesson A Summary • To refer to an element in a one-dimensional array: – Use the array’s name followed by the element’s subscript – The subscript is specified in a set of parentheses immediately following the array name • To declare a one-dimensional array, use either of these syntax versions: – Version 1: {Dim | Private | Static} array. Name(highest. Subscript) As data. Type – Version 2: {Dim | Private | Static} array. Name() As data. Type = {initial. Values} Programming with Microsoft Visual Basic 2012 24

Lesson A Summary (cont. ) • To determine the number of elements in a onedimensional array: – Use the array’s Length property or add the number 1 to the value returned by the array’s Get. Upper. Bound method • To determine the highest subscript in a one-dimensional array: – Use the array’s Get. Upper. Bound method or subtract the number 1 from the value stored in the array’s Length property • To traverse (or look at) each element in a onedimensional array: – Use a Do…Loop, For…Next, or For Each…Next loop Programming with Microsoft Visual Basic 2012 25

Lesson A Summary (cont. ) • To process instructions for each element in a group: – Use the For Each…Next statement • To sort the values stored in a one-dimensional array in ascending order: – Use the Array. Sort method – The method’s syntax is Array. Sort(array. Name) • To reverse the order of the values stored in a onedimensional array: – Use the Array. Reverse method – The method’s syntax is Array. Reverse(array. Name) Programming with Microsoft Visual Basic 2012 26

Lesson B Objectives After studying Lesson B, you should be able to: • Associate a list box with a one-dimensional array • Use a one-dimensional array as an accumulator or a counter • Explain the relationship between the elements in parallel one-dimensional arrays • Create parallel one-dimensional arrays • Locate information in two parallel one-dimensional arrays Programming with Microsoft Visual Basic 2012 27

Arrays and Collections • Items in a list box belong to a collection • Collections and arrays – Groups of individual objects treated as one unit – Each object is identified by a unique number – The first index in a collection and the first array subscript are both 0 • List boxes can be associated with arrays Programming with Microsoft Visual Basic 2012 28

Arrays and Collections (cont. ) Figure 9 -22 Problem specification for the Rose Performing Arts Center application Figure 9 -23 Illustration of the relationship between the list box and array Programming with Microsoft Visual Basic 2012 29

Arrays and Collections (cont. ) Figure 9 -25 Ticket price displayed in the interface Figure 9 -24 Most of the code for the Rose Performing Arts Center application Programming with Microsoft Visual Basic 2012 30

Arrays and Collections (cont. ) Figure 9 -26 Result of the run time error caused by an invalid subscript Programming with Microsoft Visual Basic 2012 31

Arrays and Collections (cont. ) Figure 9 -27 Modified btn. Display_Click procedure Programming with Microsoft Visual Basic 2012 32

Accumulator and Counter Arrays • Accumulator arrays – Numeric values used for accumulating something (adding together) • Counter arrays – Numeric values used for counting something (how many) • Warren School application: Figure 9 -28 Problem specification for the Warren School application Programming with Microsoft Visual Basic 2012 33

Accumulator and Counter Arrays (cont. ) Figure 9 -30 Array values displayed in the interface Figure 9 -29 btn. Add_Click procedure Programming with Microsoft Visual Basic 2012 34

Parallel One-Dimensional Arrays • Parallel arrays – Two or more arrays whose elements are related by their position in arrays (by their subscripts) – Arrays may be different data types • Scenario involving two parallel arrays: – Parallel arrays named str. Ids and int. Prices – Each str. Ids element corresponds to the int. Prices element located in the same position – Search the str. Ids array for the product ID – View the corresponding element in the int. Prices array Programming with Microsoft Visual Basic 2012 35

Parallel One-Dimensional Arrays (cont. ) Figure 9 -31 Problem specification for the Treasures Gift Shoppe Figure 9 -32 Illustration of two parallel one-dimensional arrays Programming with Microsoft Visual Basic 2012 36

Parallel One-Dimensional Arrays (cont. ) Figure 9 -34 Pseudocode and flowchart for the btn. Display_Click procedure (continues) Figure 9 -33 User interface for the Treasures Gift Shoppe application Programming with Microsoft Visual Basic 2012 37

Parallel One-Dimensional Arrays (cont. ) Figure 9 -34 Pseudocode and flowchart for the btn. Display_Click procedure Programming with Microsoft Visual Basic 2012 38

Parallel One-Dimensional Arrays (cont. ) Figure 9 -36 Sample run of the Treasures Gift Shoppe application Programming with Microsoft Visual Basic 2012 Figure 9 -35 Most of the code for the Treasures Gift Shoppe application 39

The Die Tracker Application Figure 9 -37 User interface for the Die Tracker application Programming with Microsoft Visual Basic 2012 40

The Die Tracker Application (cont. ) Figure 9 -38 Illustration of the three parallel arrays Programming with Microsoft Visual Basic 2012 41

The Die Tracker Application (cont. ) Figure 9 -40 Sample run of the Die Tracker application Programming with Microsoft Visual Basic 2012 Figure 9 -39 Most of the code for the Die Tracker application 42

Lesson B Summary • To associate the items in a list box with the elements in an array: – Use each list box item’s index and each array element’s subscript • To create parallel one-dimensional arrays: – Create two or more one-dimensional arrays – When assigning values to the arrays, be sure that the value stored in each element in the first array corresponds to the value stored in the same element in the other arrays Programming with Microsoft Visual Basic 2012 43

Lesson C Objectives After studying Lesson C, you should be able to: • • Declare and initialize a two-dimensional array Store data in a two-dimensional array Sum the values in a two-dimensional array Search a two-dimensional array Programming with Microsoft Visual Basic 2012 44

Two-Dimensional Arrays • Two-dimensional array – Resembles a table – Stores variables (elements) in rows and columns • To identify a two-dimensional array element: – Use a unique combination of two subscripts to specify an element’s row and column position • Subscripts begin at 0 • Example: str. Products(1, 2)refers to the second row, third column Programming with Microsoft Visual Basic 2012 45

Two-Dimensional Arrays (cont. ) Figure 9 -45 Names of some of the elements in the str. Cds array Programming with Microsoft Visual Basic 2012 46

Two-Dimensional Arrays (cont. ) Figure 9 -46 Syntax versions and examples of declaring a two-dimensional array Programming with Microsoft Visual Basic 2012 47

Two-Dimensional Arrays (cont. ) Figure 9 -47 Examples of statements used to store data in a two-dimensional array Programming with Microsoft Visual Basic 2012 48

Two-Dimensional Arrays (cont. ) Figure 9 -48 Syntax and an example of a two-dimensional array’s Get. Upper. Bound method Programming with Microsoft Visual Basic 2012 49

Two-Dimensional Arrays (cont. ) Traversing a Two-Dimensional Array • One loop is used to traverse a one-dimensional array • Two loops are used to traverse a two-dimensional array – An outer loop and a nested loop – One keeps track of the row subscript – One keeps track of the column subscript • You can also traverse a two-dimensional array using one For Each…Next loop – This method cannot permanently modify values Programming with Microsoft Visual Basic 2012 50

Two-Dimensional Arrays (cont. ) Traversing a Two-Dimensional Array (cont. ) Figure 9 -49 Examples of loops used to traverse a two-dimensional array Programming with Microsoft Visual Basic 2012 51

Totaling the Values Stored in a Two. Dimensional Array Figure 9 -50 Problem specification for the Jenko Booksellers application Figure 9 -52 Total sales displayed in the interface Figure 9 -51 btn. Calc_Click procedure Programming with Microsoft Visual Basic 2012 52

Searching a Two-Dimensional Array • Two-dimensional arrays versus parallel arrays – Both can represent data in tabular format – All data in a two-dimensional array must be the same type • New version of the Treasures Gift Shoppe application – Uses one two-dimensional array to store the price list – A two-dimensional array replaces two parallel arrays in the first version Programming with Microsoft Visual Basic 2012 53

Searching a Two-Dimensional Array (cont. ) Figure 9 -54 Interface showing the price for item ID KW 10 Figure 9 -53 Most of the code for the Treasures Gift Shoppe application Programming with Microsoft Visual Basic 2012 54

Lesson C Summary • To declare a two-dimensional array, use either of the syntax versions: – Version 1: {Dim | Private | Static} array. Name(highest. Row. Subscript, highest. Column. Subscript) As data. Type – Version 2: {Dim | Private | Static} array. Name(, ) As data. Type = {{initial. Values}, …{initial. Values}} • To refer to an element in a two-dimensional array: – Use the syntax array. Name(row. Subscript, column. Subscript) Programming with Microsoft Visual Basic 2012 55

Lesson C Summary (cont. ) • To determine the highest row subscript in a twodimensional array: – Use the Get. Upper. Bound method: array. Name. Get. Upper. Bound(0) • To determine the highest column subscript in a twodimensional array: – Use the Get. Upper. Bound method: array. Name. Get. Upper. Bound(1) Programming with Microsoft Visual Basic 2012 56
- Slides: 56