Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays

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

3 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

4 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

5 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 20125 Arrays

6 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 20126 One-Dimensional Arrays

7 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 20127 One-Dimensional Arrays (cont.)

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

9 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 20129 One-Dimensional Arrays (cont.) Figure 9-4 Illustration of the strStates and dblPays arrays

10 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 201210 One-Dimensional Arrays (cont.) Figure 9-5 Examples of statements used to store data in a one-dimensional array

11 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 201211 Figure 9-6 Syntax and an example of a one-dimensional array’s Length property One-Dimensional Arrays (cont.)

12 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 201212 Figure 9-7 Syntax and an example of a one-dimensional array’s GetUpperBound method One-Dimensional Arrays (cont.)

13 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 201213 Figure 9-8 Examples of loops used to traverse a one- dimensional array One-Dimensional Arrays (cont.)

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

15 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 201215 The For Each…Next Statement Figure 9-10 Syntax and an example of the For Each…Next statement

16 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 201216 Calculating the Total and Average Values Figure 9-11 Problem specification for the Brewers Coffee application

17 Programming with Microsoft Visual Basic 201217 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

18 Programming with Microsoft Visual Basic 201218 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)

19 Programming with Microsoft Visual Basic 201219 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

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

21 Programming with Microsoft Visual Basic 201221 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

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

23 Programming with Microsoft Visual Basic 201223 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

24 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 201224

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

26 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 201226

27 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 201227

28 Programming with Microsoft Visual Basic 201228 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

29 Programming with Microsoft Visual Basic 201229 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

30 Programming with Microsoft Visual Basic 201230 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

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

32 Programming with Microsoft Visual Basic 201232 Arrays and Collections (cont.) Figure 9-27 Modified btnDisplay_Click procedure

33 Programming with Microsoft Visual Basic 201233 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

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

35 Programming with Microsoft Visual Basic 201235 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

36 Programming with Microsoft Visual Basic 201236 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

37 Programming with Microsoft Visual Basic 201237 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

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

39 Programming with Microsoft Visual Basic 201239 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

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

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

42 Programming with Microsoft Visual Basic 201242 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

43 Programming with Microsoft Visual Basic 201243 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

44 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 201244

45 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 201245

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

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

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

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

50 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 201250

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

52 Totaling the Values Stored in a Two- Dimensional Array Programming with Microsoft Visual Basic 201252 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

53 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 201253

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

55 Programming with Microsoft Visual Basic 201255 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)

56 Programming with Microsoft Visual Basic 201256 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)


Download ppt "Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays."

Similar presentations


Ads by Google