Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays
Previewing the Die Tracker Application Programming with Microsoft Visual Basic Figure 9-1 Result of clicking the Roll button the first time
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 20123
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 20124
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 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: strCities(0) is the first variable in the strCities array Programming with Microsoft Visual Basic One-Dimensional Arrays
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 One-Dimensional Arrays (cont.)
Declaring a One-Dimensional Array (cont.) Programming with Microsoft Visual Basic One-Dimensional Arrays (cont.) Figure 9-3 Syntax versions and examples of declaring a one-dimensional array
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 TryParse statement –Example syntax using an assignment statement: arrayname(subscript) = value Programming with Microsoft Visual Basic One-Dimensional Arrays (cont.) Figure 9-4 Illustration of the strStates and dblPays arrays
Storing Data in a One-Dimensional Array Assignment statements and statements that contain the TryParse method are used to store data in elements Programming with Microsoft Visual Basic One-Dimensional Arrays (cont.) Figure 9-5 Examples of statements used to store data in a one-dimensional array
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 Programming with Microsoft Visual Basic Figure 9-6 Syntax and an example of a one-dimensional array’s Length property 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 GetUpperBound method –Returns an integer that represents the highest subscript in the specified array dimension For a one-dimensional array, the specified dimension is 0 Programming with Microsoft Visual Basic Figure 9-7 Syntax and an example of a one-dimensional array’s GetUpperBound method 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 Programming with Microsoft Visual Basic Figure 9-8 Examples of loops used to traverse a one- dimensional array One-Dimensional Arrays (cont.)
Traversing a One-Dimensional Array (cont.) Programming with Microsoft Visual Basic Figure 9-9 Sample run of the States application One-Dimensional Arrays (cont.)
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 Programming with Microsoft Visual Basic The For Each…Next Statement Figure 9-10 Syntax and an example of the For Each…Next statement
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 btnCalc 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 Programming with Microsoft Visual Basic Calculating the Total and Average Values Figure 9-11 Problem specification for the Brewers Coffee application
Programming with Microsoft Visual Basic Calculating the Total and Average Values (cont.) Figure 9-12 Examples of accumulating the array values Figure 9-13 Total and average amounts shown in the interface
Programming with Microsoft Visual Basic Finding the Highest Value Figure 9-14 Problem specification for the Car Emporium application Figure 9-15 Pseudocode and flowchart for the btnGet_Click procedure (continues)
Programming with Microsoft Visual Basic Finding the Highest Value (cont.) Figure 9-16 Sample run of the Car Emporium application Figure 9-15 Pseudocode and flowchart for the btnGet_Click procedure
Programming with Microsoft Visual Basic Finding the Highest Value (cont.) Figure 9-17 Get Highest button’s Click event procedure
Programming with Microsoft Visual Basic 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(arrayName) 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 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 Sorting a One-Dimensional Array (cont.) Figure 9-20 Most of the Continent application’s code Figure 9-21 Continent names displayed in ascending order
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} arrayName(highestSubscript) As dataType –Version 2: {Dim | Private | Static} arrayName() As dataType = {initialValues} Programming with Microsoft Visual Basic
Lesson A Summary (cont.) To determine the number of elements in a one- dimensional array: –Use the array’s Length property or add the number 1 to the value returned by the array’s GetUpperBound method To determine the highest subscript in a one-dimensional array: –Use the array’s GetUpperBound 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 one- dimensional array: –Use a Do…Loop, For…Next, or For Each…Next loop Programming with Microsoft Visual Basic
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(arrayName) To reverse the order of the values stored in a one- dimensional array: –Use the Array.Reverse method –The method’s syntax is Array.Reverse(arrayName) Programming with Microsoft Visual Basic
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
Programming with Microsoft Visual Basic 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 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 Arrays and Collections (cont.) Figure 9-24 Most of the code for the Rose Performing Arts Center application Figure 9-25 Ticket price displayed in the interface
Programming with Microsoft Visual Basic Arrays and Collections (cont.) Figure 9-26 Result of the run time error caused by an invalid subscript
Programming with Microsoft Visual Basic Arrays and Collections (cont.) Figure 9-27 Modified btnDisplay_Click procedure
Programming with Microsoft Visual Basic 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 Accumulator and Counter Arrays (cont.) Figure 9-29 btnAdd_Click procedure Figure 9-30 Array values displayed in the interface
Programming with Microsoft Visual Basic 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 strIds and intPrices –Each strIds element corresponds to the intPrices element located in the same position –Search the strIds array for the product ID –View the corresponding element in the intPrices array
Programming with Microsoft Visual Basic 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 Parallel One-Dimensional Arrays (cont.) Figure 9-34 Pseudocode and flowchart for the btnDisplay_Click procedure (continues) Figure 9-33 User interface for the Treasures Gift Shoppe application
Programming with Microsoft Visual Basic Parallel One-Dimensional Arrays (cont.) Figure 9-34 Pseudocode and flowchart for the btnDisplay_Click procedure
Programming with Microsoft Visual Basic Parallel One-Dimensional Arrays (cont.) Figure 9-35 Most of the code for the Treasures Gift Shoppe application Figure 9-36 Sample run of the Treasures Gift Shoppe application
Programming with Microsoft Visual Basic The Die Tracker Application Figure 9-37 User interface for the Die Tracker application
Programming with Microsoft Visual Basic The Die Tracker Application (cont.) Figure 9-38 Illustration of the three parallel arrays
Programming with Microsoft Visual Basic The Die Tracker Application (cont.) Figure 9-39 Most of the code for the Die Tracker application Figure 9-40 Sample run of the Die Tracker application
Programming with Microsoft Visual Basic 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
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
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: strProducts(1,2) refers to the second row, third column Programming with Microsoft Visual Basic
Two-Dimensional Arrays (cont.) Programming with Microsoft Visual Basic Figure 9-45 Names of some of the elements in the strCds array
Two-Dimensional Arrays (cont.) Programming with Microsoft Visual Basic Figure 9-46 Syntax versions and examples of declaring a two-dimensional array
Two-Dimensional Arrays (cont.) Programming with Microsoft Visual Basic Figure 9-47 Examples of statements used to store data in a two-dimensional array
Two-Dimensional Arrays (cont.) Programming with Microsoft Visual Basic Figure 9-48 Syntax and an example of a two-dimensional array’s GetUpperBound method
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
Two-Dimensional Arrays (cont.) Programming with Microsoft Visual Basic Figure 9-49 Examples of loops used to traverse a two-dimensional array Traversing a Two-Dimensional Array (cont.)
Totaling the Values Stored in a Two- Dimensional Array Programming with Microsoft Visual Basic Figure 9-51 btnCalc_Click procedure Figure 9-50 Problem specification for the Jenko Booksellers application Figure 9-52 Total sales displayed in the interface
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
Searching a Two-Dimensional Array (cont.) Programming with Microsoft Visual Basic Figure 9-53 Most of the code for the Treasures Gift Shoppe application Figure 9-54 Interface showing the price for item ID KW10
Programming with Microsoft Visual Basic Lesson C Summary To declare a two-dimensional array, use either of the syntax versions: –Version 1: {Dim | Private | Static} arrayName(highestRowSubscript, highestColumnSubscript) As dataType –Version 2: {Dim | Private | Static} arrayName(,) As dataType = {{initialValues},…{initialValues}} To refer to an element in a two-dimensional array: –Use the syntax arrayName(rowSubscript, columnSubscript)
Programming with Microsoft Visual Basic Lesson C Summary (cont.) To determine the highest row subscript in a two- dimensional array: –Use the GetUpperBound method: arrayName.GetUpperBound(0) To determine the highest column subscript in a two- dimensional array: –Use the GetUpperBound method: arrayName.GetUpperBound(1)