Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u...

25
Arrays Chapter 8

Transcript of Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u...

Page 1: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Arrays

Chapter 8

Page 2: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Overview

General discussion Variable arrays Control arrays Multi-dimensional variable arrays

Two-dimensional Three-dimensional

Parallel arrays Dynamic arrays

Page 3: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Arrays

Often a set of objects or variables of the same type are used to hold related data Prices of food items at a fast food restaurant Scores on programming assignments

In the GPA calculator, rather than having five textboxes with different names to enter the grades, we might better describe the situation as an array of elements Many data containers One name

Page 4: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

3

Variable Arrays A variable like Stuff

defines a data object into which a single quantity can be placed

A variable like Product defines a data object into which an array of information can be placed

210 Product

Stuff

Page 5: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Declaring Variable Arrays

An array is declared using the following syntax: Dim ArrayName(LowerBound To UpperBound) as VariableType

The LowerBound of the array is the lowest index in the array, while the UpperBound is the highest index in the array

Page 6: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Variable Array – Example (1)

Arrays are useful for when there are many instances of similar data

Instead of ten integer variables holding ten values, a single array can handle all ten

intdintc inte

intj

intf

intb

intainti

inthintgintArray

Page 7: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Variable Array – Example (2)

Each variable location in an array is called an element Use Dim statement to define size and type

Dim intArray(1 to 10) as Integer ‘creates the structure below

Treat as normal variables Reference using subscripts: intArray(4) = 114

intArray

114

Page 8: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Variable Array – Example (3)

Arrays allow programmers to write code once, but have it applied to many different data elements

Compare adding all the integers stored in the variables…

intdintc inte

intj

intf

intb

intainti

inthintgintArray

intSum = inta + intb + intc _+ intd + inte + intf + intg _+ inth + inti + inj

Page 9: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Variable Array – Example (4)

Arrays allow programmers to write code once, but have it applied to many different data elements

…to adding all the elements of the array

intdintc inte

intj

intf

intb

intainti

inthintgintArray

For intI = 1 to 10 intSum = intSum + intArray(intI)Next intI

Page 10: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Control Arrays

VB allows the programmer to create arrays of controls on the form Just as there are variable arrays, there may be arrays of objects,

such as command buttons, labels, textboxes or picture boxes An array of labels might be made with one label for each grade

point An array of textboxes might be made with one textbox for each

grade, or credits

Characteristics Name with subscripted elements .Index property is the subscript (Properties Window)

Page 11: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Control Arrays

Using the same procedure, you can make arrays of labels, or of pictures, or of check boxes, etc.

You can change the GPA calculator program to make arrays of grades, credits and grade points

Page 12: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating a Control Array – Example

Create an toolbox object, perhaps a command button, changing its (name) and caption.

Page 13: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating a Control Array – Example1. Copy and paste the object (Ctrl-C)(Ctrl-V) or Edit/Copy then Edit/Paste

2. The message “Do you want to create a control array?” appears. Answer “yes”

3. Type Edit/Paste or (Ctrl-V) several more times to make an array of commands

Page 14: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating a Control Array – Example

The Properties Window shows it all.

Note that the 4th command created is shown as “cmdButn(3)”

Page 15: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating a Control Array – Example

Arrange the buttons on the form as desired

Double clicking on any one of them opens the code window

Page 16: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating a Control Array – Example

The event handler is a bit strange. There is now a “parameter” inside the normally blank parentheses:

Private Sub cmdButn_Click(Index As Integer)

lblOutput.Caption = "## " & Index & " ##"

End Sub

Page 17: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating a Control Array – Example

Run the process

Click each button

Observe the effect

Page 18: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating Multi-dimensional variable Arrays

Arrays can come in different dimensions

You need to specify the lower and upper bounds for each dimension Array name; Dimensions; Bounds; Type An example of a two-dimensional array:

Dim strName (1 to 3, 1 to 4) as String

Page 19: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Advanced Variable Arrays

Multi-dimensional arrays Parallel arrays Searching arrays Dynamic Arrays

Page 20: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Creating Multi-dimensional variable Arrays

When accessing/storing elements in the array… Any integer literal, variable or expression can be used to

subscript Must stay within the bounds Must have the correct number of subscripts If storing, must assign the correct type of data

Given: Dim strName (1 to 3, 1 to 4) as String Is strName (0, 0) = “Jones” valid? Is strName (2) = “Smith” valid? Is strName (4, 3) = “Larry” valid? Is strName(3, 4) = 1402 valid?

Page 21: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Two-dimensional variable Arrays Two dimensional arrays

require two subscripts elements are listed (row, column)

ExampleDim intSales(1 to 3, 1 to 4) _as Integer

What is intSales(2, 3)?1

2

3

1 2 3 4

1402

7532

Page 22: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Three-dimensional variable Arrays

Three dimensional arrays require three subscripts (row, column, depth)

Example Dim intSales (1 to 3, 1 to 4, 1 to 2) as Integer

1402

2134

4521

3425

So the number 2134 is found where? the number 4521 is ______ the number 1402 is ______ the number 3425 is ______

Page 23: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Parallel Arrays

Parallel arrays are used to hold related data with different types Dim strName (1 to 5) as String Dim intSales (1 to 5) as Integer So, if we know that Smith is in

strName(2), we also know his or her sales is in intSales(2)

strName intSales

Jones

Smith

Frank

Able

Zean

1402

2301

0231

6762

0199

Page 24: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Searching Arrays

strTarget = "Q" intFound = 0For intI = 1 to 10 If strTarget = strArray(intI) Then intFound = intI Exit For End IfNext intI strArray

A F S V W Q Z X Y L

• Searching implies looking for a target• A simple sequential search looks through the

array for the target

Page 25: Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Dynamic Arrays You can re-dimension an array at run-time Rules for resizing arrays:

Use can empty dimension list: Dim intArray() as Integer Use ReDim to assign dimensions:

ReDim intArray (1 to intN, 1 to 2) Use ReDim to change Bounds

ReDim intArray (1 to (intN + 10), 1 to 3)

Use Preserve keyword to save data in arrayReDim Preserve intArray(1 to intN, 1 to

5)