lesson 8 arrays
TRANSCRIPT
-
7/26/2019 Lesson 8 Arrays
1/58
ARRAYS
-
7/26/2019 Lesson 8 Arrays
2/58
PREVIEWINGTHETREASURESGIFTSHOP
APPLICATION
Treasures application
Allows the user to enter a product ID
Displays the products price
Open the Treasures.exe file
2
-
7/26/2019 Lesson 8 Arrays
3/58
PREVIEWINGTHETREASURESGIFTSHOP
APPLICATION(CONTD.)
3
Figure 9-1 Interface showing the products price
-
7/26/2019 Lesson 8 Arrays
4/58
LESSONA 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 thehighest subscript
Traverse a one-dimensional array
Code a loop using the For EachNext statement
4
-
7/26/2019 Lesson 8 Arrays
5/58
LESSONA OBJECTIVES(CONTD.)
Compute the total and average of a one-
dimensional arrays contents
Find the highest value in a one-dimensional
array
Associate a list box with a one-dimensional array
Use a one-dimensional array as an accumulator
or a counter
Sort a one-dimensional array
5
-
7/26/2019 Lesson 8 Arrays
6/58
USINGARRAYS
Simple (scalar) variable
One that is unrelated to any other variable in
memory
Array
Group of related variables
Variables have same name and data type
Reasons to use arrays
Simplifies process of coding application
Increases run-time efficiency of program
6
-
7/26/2019 Lesson 8 Arrays
7/58
ONE-DIMENSIONALARRAYS
One-dimensional array
Variables stored in consecutive memory locations
Visualized as a column of variables
Subscript
Indicates variables position in the array
Starts at 0 for first array variable
Refer to array variable by array name and
subscript
Example: strCities(0)is first variable in
strCities array
7
-
7/26/2019 Lesson 8 Arrays
8/58
ONE-DIMENSIONALARRAYS(CONTD.)
8
Figure 9-2 Illustration of the one-dimensional strCitiesarray
-
7/26/2019 Lesson 8 Arrays
9/58
9Figure 9-3 Syntax versions and examples of declaring a one-dimensional array
-
7/26/2019 Lesson 8 Arrays
10/58
ONE-DIMENSIONALARRAYS(CONTD.)
Element: Refers to individual array variable
Arrays are initialized by computer when created
Arrays of numeric variables are initialized to 0
Arrays of string variables are initialized usingkeyword Nothing
Arrays of Boolean variables are initialized to False
Arrays of Date variables are initialized to 12:00 AM
January 1, 0001
Populating the array Assigning initial values
10
-
7/26/2019 Lesson 8 Arrays
11/58
ONE-DIMENSIONALARRAYS(CONTD.)
After array is declared, you can store data in
array
To enter data into array:
Use assignment statement
Use TryParse statement
Example syntax using assignment statement
arrayname(subscript) = value
11
-
7/26/2019 Lesson 8 Arrays
12/58
12Figure 9-4 Examples of statements used to store data in a one-dimensional array
-
7/26/2019 Lesson 8 Arrays
13/58
DETERMININGTHENUMBEROF
ELEMENTSANDTHEHIGHESTSUBSCRIPT
Arrays Length property
Stores number of elements in array
Number is an integer
Highest subscript in a one-dimensional array
Subtract 1 from arrays Length property
GetUpperBound method
Returns an integer that represents highest subscript
in specified array dimension
For one-dimensional array, specified dimension is 0
13
-
7/26/2019 Lesson 8 Arrays
14/58
DETERMININGTHENUMBEROFELEMENTS
ANDTHEHIGHESTSUBSCRIPT(CONTD.)
14
Figure 9-5 Syntax and an example of a one-dimensional arrays Length property
-
7/26/2019 Lesson 8 Arrays
15/58
DETERMININGTHENUMBEROFELEMENTS
ANDTHEHIGHESTSUBSCRIPT(CONTD.)
15
Figure 9-6 Syntax and an example of a one-dimensional arrays GetUpperBound method
-
7/26/2019 Lesson 8 Arrays
16/58
TRAVERSINGAONE-DIMENSIONALARRAY
Traversing an array
Look at each array element, one by one
Beginning with first element
Ending with last element
Figure 9-7 shows two examples
16
-
7/26/2019 Lesson 8 Arrays
17/58
TRAVERSINGAONE-DIMENSIONALARRAY
(CONTD.)
17
Figure 9-7 Examples of loops used to traverse a one-dimensional array
-
7/26/2019 Lesson 8 Arrays
18/58
THEFOREACHNEXTSTATEMENT
For EachNext statement
Used to process each element in array
Unlike ForNext statement:
You do not have to keep track of array subscripts
Can only read array values, not permanently modify
them
Declare variable within For EachNext
statement to refer to each array element, one at a
time
18
-
7/26/2019 Lesson 8 Arrays
19/58
19
Figure 9-9 Syntax and an example of the For EachNext statement
-
7/26/2019 Lesson 8 Arrays
20/58
CALCULATINGTHETOTALANDAVERAGE
VALUES
Sweet Tooth Chocolate application
Displays total number of pounds of chocolate sold
during six month period
Displays average number of pounds sold each month
btnCalc controls Click event procedure
Adds array element values
Divides total by number of array elements
Displays average amount on form
20
-
7/26/2019 Lesson 8 Arrays
21/58
21
Figure 9-11
Examples of code for the
btnCalc_Click event procedure
(continues)
-
7/26/2019 Lesson 8 Arrays
22/58
22
Figure 9-11
Examples of code for the btnCalc_Click event procedure (contd.)
-
7/26/2019 Lesson 8 Arrays
23/58
FINDINGTHEHIGHESTVALUE
Cycles Galore application
Displays highest bonus amount earned during the
month
Displays number of salespeople who earned that
amount
Get Highest Bonus buttons Click event
procedure
Code shown in Figure 9-16
23
-
7/26/2019 Lesson 8 Arrays
24/58
24Figure 9-16 Get Highest Bonus buttons Click event procedure
-
7/26/2019 Lesson 8 Arrays
25/58
ARRAYSANDCOLLECTIONS
Items in a list box belong to a collection
Collections and arrays
Group of individual objects treated as one unit
Each object identified by unique number
First index in a collection and first array subscript
are both 0
List boxes can be associated with arrays
25
-
7/26/2019 Lesson 8 Arrays
26/58
ARRAYSANDCOLLECTIONS(CONTD.)
Prairie Auditorium application
26Figure 9-17 Problem specification for the Prairie Auditorium application
-
7/26/2019 Lesson 8 Arrays
27/58
ARRAYSANDCOLLECTIONS(CONTD.)
27
Figure 9-18 Illustration of the relationship between the list box and array
-
7/26/2019 Lesson 8 Arrays
28/58
28
Figure 9-19 Most of the code for the Prairie Auditorium application
-
7/26/2019 Lesson 8 Arrays
29/58
ACCUMULATORANDCOUNTERARRAYS
One-dimensional arrays
Often used to either accumulate or count related
values
Warren School application
Uses an accumulator array
Allows user to enter amount of candy sold for each
student
Displays total number sold for each candy
29
-
7/26/2019 Lesson 8 Arrays
30/58
30
Figure 9-24 btnAdd controls Click event procedure
-
7/26/2019 Lesson 8 Arrays
31/58
SORTINGAONE-DIMENSIONALARRAY
Sorting
Arranging data in a specific order
Array.Sort method
Sorts elements of one-dimensional array in ascending
order
Syntax: Array.Sort(arrayname)
To sort array in descending order:
First use Array.Sort to sort in ascending order
Then useArray.Reverseto reverse order of array
elements
31
-
7/26/2019 Lesson 8 Arrays
32/58
32Figure 9-26 Syntax and examples of the Array.Sort and Array.Reverse methods
-
7/26/2019 Lesson 8 Arrays
33/58
SORTINGAONE-DIMENSIONALARRAY
(CONTD.)
33Figure 9-28 State names displayed in ascending order
-
7/26/2019 Lesson 8 Arrays
34/58
LESSONA SUMMARY
An array groups variables with same data type
under one name
Individual array variables also called elements
Arrays may be declared with or without list of
initial values
Values can be assigned after declaration
Arrays Length property returns array size
Can also use GetUpperBound method
34
-
7/26/2019 Lesson 8 Arrays
35/58
LESSONA SUMMARY(CONTD.)
To traverse each element in a one-dimensional
array
Use a loop coded with one of the following
statements:
DoLoop, ForNext, For EachNext
To associate list box with array:
Use list box items index and array elements
subscript
Array.Sort: Sorts elements in ascending orderArray.Reverse: Reverses order of array elements
35
-
7/26/2019 Lesson 8 Arrays
36/58
LESSONB OBJECTIVES
After studying Lesson B, you should be able to:
Explain the relationship between the elements in
parallel one-dimensional arrays
Create parallel one-dimensional arrays
Locate information in two parallel one-
dimensional arrays
36
-
7/26/2019 Lesson 8 Arrays
37/58
PARALLELONE-DIMENSIONALARRAYS
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 strIdsand intPrices
Each strIdselement corresponds to intPrices
element located in same position
Search strIdsarray for product ID View corresponding element in intPricesarray
37
-
7/26/2019 Lesson 8 Arrays
38/58
PARALLELONE-DIMENSIONALARRAYS
(CONTD.)
38
Figure 9-31 Illustration of two parallel one-dimensional arrays
-
7/26/2019 Lesson 8 Arrays
39/58
39
Figure 9-32 btnDisplay controls Click event procedure
using parallel one-dimensional arrays (continues)
-
7/26/2019 Lesson 8 Arrays
40/58
40
Figure 9-32 btnDisplay controls Click event procedure
using parallel one-dimensional arrays (contd.)
-
7/26/2019 Lesson 8 Arrays
41/58
PARALLELONE-DIMENSIONALARRAYS
(CONTD.)
41
Figure 9-33 Interface showing the price for product ID CR20
-
7/26/2019 Lesson 8 Arrays
42/58
LESSONB SUMMARY
To create parallel one-dimensional arrays:
Create two or more one-dimensional arrays
Ensure value stored in first element in first array
corresponds to value stored in same element in other
arrays
42
-
7/26/2019 Lesson 8 Arrays
43/58
LESSONC 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
43
-
7/26/2019 Lesson 8 Arrays
44/58
TWO-DIMENSIONALARRAYS
Two-dimensional array
Resembles a table
Storing variables (elements) in rows and columns
Identifying a two-dimensional array element
Use unique combination of two subscripts to specify
elements row and column position
Subscripts begin at 0
Example: strProducts(1,2)refers to second row, third
column
44
-
7/26/2019 Lesson 8 Arrays
45/58
TWO-DIMENSIONALARRAYS(CONTD.)
45
Figure 9-37 Names of some of the elements in the strProducts array
-
7/26/2019 Lesson 8 Arrays
46/58
46Figure 9-38 Syntax versions and examples of declaring a two-dimensional array (continues)
-
7/26/2019 Lesson 8 Arrays
47/58
47
Figure 9-38 Syntax versions and examples of declaring a two-dimensional array (contd.)
-
7/26/2019 Lesson 8 Arrays
48/58
48
Figure 9-39
Examples of
statements used to
store data in a two-dimensional array
(continues)
-
7/26/2019 Lesson 8 Arrays
49/58
49
Figure 9-39 Examples of statements used to store data in a two-dimensional array (contd.)
-
7/26/2019 Lesson 8 Arrays
50/58
50
Figure 9-40 Syntax and an example of a two-dimensional arrays GetUpperBound method
-
7/26/2019 Lesson 8 Arrays
51/58
TRAVERSINGATWO-DIMENSIONALARRAY
One loop used to traverse one-dimensional array
Two loops used to traverse two-dimensional array
Outer loop and nested loop
One keeps track of row subscript
One keeps track of column subscript
Can also traverse two-dimensional array using:
One For EachNext loop
This method cannot permanently modify values
51
-
7/26/2019 Lesson 8 Arrays
52/58
52
Figure 9-41 Examples of loops used to traverse a two-dimensional array (continues)
-
7/26/2019 Lesson 8 Arrays
53/58
53Figure 9-41 Examples of loops used to traverse a two-dimensional array (contd.)
-
7/26/2019 Lesson 8 Arrays
54/58
SEARCHINGATWO-DIMENSIONALARRAY
Two-dimensional arrays versus parallel arrays
Both can represent data in tabular format
All data in two-dimensional array must be same type
New version of Treasures application
Use one two-dimensional array to store price list
2-D array replaces two parallel arrays in first version
54
-
7/26/2019 Lesson 8 Arrays
55/58
55
Figure 9-45 btnDisplay controls Click event procedure using a
two-dimensional array (continues)
-
7/26/2019 Lesson 8 Arrays
56/58
56
Figure 9-45 btnDisplay controls Click event procedure using a
two-dimensional array (contd.)
-
7/26/2019 Lesson 8 Arrays
57/58
LESSONC SUMMARY
To declare a two-dimensional array:
Use either syntax:
To refer to two-dimensional array element:
Use syntax:
arrayName(rowSubscript, columnSubscript)
57
-
7/26/2019 Lesson 8 Arrays
58/58
LESSONC SUMMARY(CONTD.)
To determine highest row and column subscripts
in a two-dimensional array:
Use GetUpperBound method
Syntax:
arrayName.GetUpperBound(0)
arrayName.GetUpperBound(1)
58