Download presentation
Presentation is loading. Please wait.
1
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays
2
Microsoft Visual Basic 2005: Reloaded, Second Edition2 Using Arrays Simple variable (or scalar variable): a variable that is unrelated to any other variable in memory Array: –A group of variables with the same name and data type that are related in some way –Used to temporarily store related data in memory –Increases the efficiency of a program Commonly used arrays: –One-dimensional –Two-dimensional
3
Microsoft Visual Basic 2005: Reloaded, Second Edition3 One-Dimensional Arrays One-dimensional array: –Can be viewed as a column of variables Subscript: –A unique number that identifies each variable in a one-dimensional array –Starts at 0 for first element in the array Use array name and subscript to refer to each individual variable in the array
4
Microsoft Visual Basic 2005: Reloaded, Second Edition4 One-Dimensional Arrays
5
Microsoft Visual Basic 2005: Reloaded, Second Edition5 One-Dimensional Arrays (continued)
6
Microsoft Visual Basic 2005: Reloaded, Second Edition6 One-Dimensional Arrays (continued)
7
Microsoft Visual Basic 2005: Reloaded, Second Edition7 One-Dimensional Arrays (continued) Element: an individual variable in the array When an array is declared: –Each element in the array is initialized if no values are provided –Initial values can be specified for elements
8
Microsoft Visual Basic 2005: Reloaded, Second Edition8 Storing Data in a One-Dimensional Array
9
Microsoft Visual Basic 2005: Reloaded, Second Edition9 Manipulating One-Dimensional Arrays Array elements can be used like any other variable Examples: –Display the contents of an array –Access an array element using its subscript –Search the array –Calculate the average of data stored in a numeric array –Find the highest value stored in an array –Update array elements –Sort array elements
10
Microsoft Visual Basic 2005: Reloaded, Second Edition10 Displaying the Contents of a One-Dimensional Array
11
Microsoft Visual Basic 2005: Reloaded, Second Edition11 Displaying the Contents of a One-Dimensional Array (continued)
12
Microsoft Visual Basic 2005: Reloaded, Second Edition12 Displaying the Contents of a One-Dimensional Array (continued)
13
Microsoft Visual Basic 2005: Reloaded, Second Edition13 The For Each…Next Statement For Each…Next statement: –Used to code a loop which processes each element in a group or array –Creates a variable used to represent each item in the group or array –Data type of the element must match the data type of the group
14
Microsoft Visual Basic 2005: Reloaded, Second Edition14 The For Each…Next Statement (continued)
15
Microsoft Visual Basic 2005: Reloaded, Second Edition15 Using the Subscript to Access an Element in a One-Dimensional Array
16
Microsoft Visual Basic 2005: Reloaded, Second Edition16 Using the Subscript to Access an Element … (continued)
17
Microsoft Visual Basic 2005: Reloaded, Second Edition17 Subscript to Access an Element …
18
Microsoft Visual Basic 2005: Reloaded, Second Edition18 Searching a One-Dimensional Array
19
Microsoft Visual Basic 2005: Reloaded, Second Edition19 Searching a One-Dimensional Array (continued)
20
Microsoft Visual Basic 2005: Reloaded, Second Edition20 Searching a One-Dimensional Array (continued)
21
Microsoft Visual Basic 2005: Reloaded, Second Edition21 Searching a One-Dimensional Array
22
Microsoft Visual Basic 2005: Reloaded, Second Edition22 Calculating the Average Amount Stored in a One-Dimensional Numeric Array Sample application: –Find the average from an array of test scores Length property: number of elements in the array
23
Microsoft Visual Basic 2005: Reloaded, Second Edition23 Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued)
24
Microsoft Visual Basic 2005: Reloaded, Second Edition24 Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued)
25
Microsoft Visual Basic 2005: Reloaded, Second Edition25 Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued)
26
Microsoft Visual Basic 2005: Reloaded, Second Edition26 Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued)
27
Microsoft Visual Basic 2005: Reloaded, Second Edition27 Determining the Highest Value Stored in a One-Dimensional Array
28
Microsoft Visual Basic 2005: Reloaded, Second Edition28 Determining the Highest Value Stored in a One-Dimensional Array (continued)
29
Microsoft Visual Basic 2005: Reloaded, Second Edition29 Determining the Highest Value Stored in a One-Dimensional Array (continued)
30
Microsoft Visual Basic 2005: Reloaded, Second Edition30 Updating the Values Stored in a One-Dimensional Array
31
Microsoft Visual Basic 2005: Reloaded, Second Edition31 Updating the Values Stored in a One-Dimensional Array (continued)
32
Microsoft Visual Basic 2005: Reloaded, Second Edition32 Updating the Values Stored in a One-Dimensional Array (continued)
33
Microsoft Visual Basic 2005: Reloaded, Second Edition33 Updating the Values Stored in a One-Dimensional Array (continued)
34
Microsoft Visual Basic 2005: Reloaded, Second Edition34 Sorting the Data Stored in a One-Dimensional Array Sorting: arranging data in a specific order –Ascending: first element is smallest, last element is largest –Descending: first element is largest, last element is smallest Array.Sort method: used to sort elements in a one- dimensional array in ascending order Array.Reverse method: used after Array.Sort method to change to descending order
35
Microsoft Visual Basic 2005: Reloaded, Second Edition35 Sorting the Data Stored in a One-Dimensional Array (continued)
36
Microsoft Visual Basic 2005: Reloaded, Second Edition36 Sorting the Data Stored in a One-Dimensional Array (continued)
37
Microsoft Visual Basic 2005: Reloaded, Second Edition37 Parallel One-Dimensional Arrays
38
Microsoft Visual Basic 2005: Reloaded, Second Edition38 Parallel One-Dimensional Arrays (continued)
39
Microsoft Visual Basic 2005: Reloaded, Second Edition39 Parallel One-Dimensional Arrays (continued) Parallel arrays: –Two or more arrays whose elements are related by their position in the arrays
40
Microsoft Visual Basic 2005: Reloaded, Second Edition40 Parallel One-Dimensional Arrays (continued)
41
Microsoft Visual Basic 2005: Reloaded, Second Edition41 Parallel One-Dimensional Arrays (continued)
42
Microsoft Visual Basic 2005: Reloaded, Second Edition42 Parallel One-Dimensional Arrays (continued)
43
Microsoft Visual Basic 2005: Reloaded, Second Edition43 Two-Dimensional Arrays Two-dimensional array: –Resembles a table with rows and columns Each element is identified by a unique combination of two subscripts: (row, column) Subscripts are zero-relative
44
Microsoft Visual Basic 2005: Reloaded, Second Edition44 Two-Dimensional Arrays (continued)
45
Microsoft Visual Basic 2005: Reloaded, Second Edition45 Two-Dimensional Arrays (continued)
46
Microsoft Visual Basic 2005: Reloaded, Second Edition46 Two-Dimensional Arrays (continued)
47
Microsoft Visual Basic 2005: Reloaded, Second Edition47 Two-Dimensional Arrays (continued) Two-dimensional array: –Declared with highest row subscript and highest column subscript (zero-relative) Number of rows = highest row subscript + 1 Number of columns = highest column subscript + 1 Can specify initial values for array elements If no initial values are declared, array elements are automatically initialized
48
Microsoft Visual Basic 2005: Reloaded, Second Edition48 Storing Data in a Two-Dimensional Array
49
Microsoft Visual Basic 2005: Reloaded, Second Edition49 Storing Data in a Two-Dimensional Array (continued)
50
Microsoft Visual Basic 2005: Reloaded, Second Edition50 Searching a Two-Dimensional Array
51
Microsoft Visual Basic 2005: Reloaded, Second Edition51 Searching a Two-Dimensional Array (continued)
52
Microsoft Visual Basic 2005: Reloaded, Second Edition52 Searching a Two-Dimensional Array (continued)
53
Microsoft Visual Basic 2005: Reloaded, Second Edition53 Searching a Two-Dimensional Array (continued)
54
Microsoft Visual Basic 2005: Reloaded, Second Edition54 Programming Tutorial
55
Microsoft Visual Basic 2005: Reloaded, Second Edition55 Programming Example
56
Microsoft Visual Basic 2005: Reloaded, Second Edition56 Summary Arrays: a group of related variables with the same name and same data type Array element: one item in the array One-dimensional array: each element is uniquely identified by its position (subscript) in the array Two-dimensional array: each element is uniquely identified by its position (row and column subscripts) in the array Subscripts are zero-relative
57
Microsoft Visual Basic 2005: Reloaded, Second Edition57 Summary (continued) Number of elements in an array is the highest subscript value + 1 For Each...Next statement: used to loop through all the elements in an array Length property: returns the number of elements in an array Array.Sort method: sorts the elements in a one- dimensional array in ascending order Array.Reverse method: reverses the order of elements in a one-dimensional array
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.