Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s...

Preview:

Citation preview

Dim ArrayName(UpperBound) As DatatypeDim ArrayName(UpperBound) As Datatype

An array is declared using the following syntax:

An array’s name obeys the same rules as when declaring a variable: it begins with a letter and may be followed by any number of letters, underscores, or digits. An array name can be as small as one letter or as large as 255 letters, underscores, and digits.

Individual values within an array are selected by using an index.

The lowest index of an array is 0, while the upper bound of the array is the highest index in the array.

An index must be a Short, an Integer, or a Long data type.

The data type is any valid variable like a Short, an Integer, or a String.

ArraysArrays

Computer Memory

shtGrades (short)Index 0 1 2 3

Values 0 0 0 0

Dim shtGrades(3) As ShortDim shtGrades(3) As Short

Computer Memory

shtGrades (short)Index 0 1 2 3

Values 21 0 0 0

shtGrades(0) = 21 shtGrades(0) = 21

We can assign an individual variable by using its name, and index.

Index and subscript are synonymous

Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount As Integer

Dim intMaxVal As Integer = 0

For intLoopCount = 0 To UBound(shtAges)

If intMaxVal < shtAges(intLoopCount) Then

intMaxVal = shtAges(intLoopCount)

End If

Next intLoopCount

bntRedDemo.Text = intMaxVal.ToString

Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount As Integer

Dim intMaxVal As Integer = 0

For intLoopCount = 0 To UBound(shtAges)

If intMaxVal < shtAges(intLoopCount) Then

intMaxVal = shtAges(intLoopCount)

End If

Next intLoopCount

bntRedDemo.Text = intMaxVal.ToString

Finding the largest value in an array

Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMaxVal As Integer = 0

For intLoopCount = 0 To UBound(shtAges)

If intMaxVal < shtAges(intLoopCount) Then

intMaxVal = shtAges(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str(intLocation)

Dim shtAges() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMaxVal As Integer = 0

For intLoopCount = 0 To UBound(shtAges)

If intMaxVal < shtAges(intLoopCount) Then

intMaxVal = shtAges(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str(intLocation)

We often want to know the location of the max value..

Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12}

Dim intLoopCount, intLocation As Integer

Dim intMaxVal As Integer = 0

For intLoopCount = 0 To UBound(shtA)

If intMaxVal < shtA(intLoopCount) Then

intMaxVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str(intLocation)

Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12}

Dim intLoopCount, intLocation As Integer

Dim intMaxVal As Integer = 0

For intLoopCount = 0 To UBound(shtA)

If intMaxVal < shtA(intLoopCount) Then

intMaxVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str(intLocation)

Our code can fail because of an incorrect assumption…

Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12}

Dim intLoopCount, intLocation As Integer

Dim intMaxVal As Integer = - 2147483648

For intLoopCount = 0 To UBound(shtA)

If intMaxVal < shtA(intLoopCount) Then

intMaxVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str(intLocation)

Dim shtA() As Short = {-21, -23, -45, -12, -23, -65, -12}

Dim intLoopCount, intLocation As Integer

Dim intMaxVal As Integer = - 2147483648

For intLoopCount = 0 To UBound(shtA)

If intMaxVal < shtA(intLoopCount) Then

intMaxVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMaxVal) & “, at ” & str(intLocation)

Here the incorrect assumption is fixed

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMinVal As Integer = 2147483647

For intLoopCount = 0 To UBound(shtA)

If intMinVal > shtA(intLoopCount) Then

intMinVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation)

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMinVal As Integer = 2147483647

For intLoopCount = 0 To UBound(shtA)

If intMinVal > shtA(intLoopCount) Then

intMinVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation)

Consider finding the minimum value, again a pessimistic assumption

Note relational test

Note pessimistic assumption

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMinVal As Integer = 2147483647

For intLoopCount = 0 To UBound(shtA)

If intMinVal >= shtA(intLoopCount) Then

intMinVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation)

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMinVal As Integer = 2147483647

For intLoopCount = 0 To UBound(shtA)

If intMinVal >= shtA(intLoopCount) Then

intMinVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation)

The previous slide found the first occurrence of the lowest value, this code finds the last occurrence, do you see the difference?

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMinVal As Integer = 2147483647

For intLoopCount = 0 To UBound(shtA)

If intMinVal >= shtA(intLoopCount) Then

intMinVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation)

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim intLoopCount, intLocation As Integer

Dim intMinVal As Integer = 2147483647

For intLoopCount = 0 To UBound(shtA)

If intMinVal >= shtA(intLoopCount) Then

intMinVal = shtA(intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Value ” & str(intMinVal ) & “, at ” & str(intLocation)

From now on I won’t bother showing this stuff, you can assume it from context

Dim intFindMe As Short = 45

Dim blnWasFound As Boolean = False

For intLoopCount = 0 To UBound(shtA)

If intFindMe = shtA(intLoopCount) Then

blnWasFound = True

intLocation = intLoopCount

End If

Next intLoopCount

If blnWasFound Then bntRedDemo.Text = "Found at " & Str(intLocation)Else bntRedDemo.Text = "Not found!"End If

Dim intFindMe As Short = 45

Dim blnWasFound As Boolean = False

For intLoopCount = 0 To UBound(shtA)

If intFindMe = shtA(intLoopCount) Then

blnWasFound = True

intLocation = intLoopCount

End If

Next intLoopCount

If blnWasFound Then bntRedDemo.Text = "Found at " & Str(intLocation)Else bntRedDemo.Text = "Not found!"End If

If IntFindMe was say, 1111

{21, 23, 45, 12, 23, 65, 12}{21, 23, 45, 12, 23, 65, 12}

Note the pessimistic assumption. Question: Does it find the first or last occurrence?

Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58}

Dim intMaxChange As Integer = 0

For intLoopCount = 0 To UBound(shtA)

If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then

intMaxChange = shtA (intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Max change at” & Str(intLocation)

Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58}

Dim intMaxChange As Integer = 0

For intLoopCount = 0 To UBound(shtA)

If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then

intMaxChange = shtA (intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Max change at” & Str(intLocation)

Lets try to find the location of the greatest (positive change)

Lets try to find the location of the greatest (positive change)

Looks good, but there is a bug!

Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58}

Dim intMaxChange As Integer = 0

For intLoopCount = 0 To UBound(shtA) - 1

If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then

intMaxChange = shtA (intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Max change at” & Str(intLocation)

Dim shtA() As Short = {25,25,50,51,52,53,53,56,57,58}

Dim intMaxChange As Integer = 0

For intLoopCount = 0 To UBound(shtA) - 1

If intMaxChange < (shtA(intLoopCount + 1) - shtA(intLoopCount)) Then

intMaxChange = shtA (intLoopCount)

intLocation = intLoopCount

End If

Next intLoopCount

bntRedDemo.Text = “Max change at” & Str(intLocation)

This fixes our bug…This fixes our bug…

Let us try to find a repeated item

Dim intInnerLoop, intOuterLoop As Integer

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim blnWasRepeat As Boolean = False

For intInnerLoop = 0 To UBound(shtA)

bntRedDemo.Text = Str(shtA(intInnerLoop))

Next intInnerLoop

Dim intInnerLoop, intOuterLoop As Integer

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim blnWasRepeat As Boolean = False

For intInnerLoop = 0 To UBound(shtA)

bntRedDemo.Text = Str(shtA(intInnerLoop))

Next intInnerLoop

21 23 45 12…

Dim intInnerLoop, intOuterLoop As Integer

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim blnWasRepeat As Boolean = False

For intOuterLoop = 0 To UBound(shtA)

For intInnerLoop = 0 To UBound(shtA)

bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop))

Next intInnerLoop

Next intOuterLoop

Dim intInnerLoop, intOuterLoop As Integer

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim blnWasRepeat As Boolean = False

For intOuterLoop = 0 To UBound(shtA)

For intInnerLoop = 0 To UBound(shtA)

bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop))

Next intInnerLoop

Next intOuterLoop

21 21 21 23 21 45 12 12…

21 21

21 24

21 12

intOuterLoop = 0 {21, 23, 45, 12, 23, 65, 12}

intInnerLoop = 0

intInnerLoop = 1

intInnerLoop = 2

Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop))

23 21

23 23

23 12

…12 21

12 23

12 12

intOuterLoop = 1

intOuterLoop = 6

{21, 23, 45, 12, 23, 65, 12}

21 21

21 23

21 12

intOuterLoop = 0

Dim intInnerLoop, intOuterLoop As Integer

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim blnWasRepeat As Boolean = False

For intOuterLoop = 0 To UBound(shtA)

If intOuterLoop <> intInnerLoop

For intInnerLoop = 0 To UBound(shtA)

bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop))

Next intInnerLoop

End If

Next intOuterLoop

Dim intInnerLoop, intOuterLoop As Integer

Dim shtA() As Short = {21, 23, 45, 12, 23, 65, 12}

Dim blnWasRepeat As Boolean = False

For intOuterLoop = 0 To UBound(shtA)

If intOuterLoop <> intInnerLoop

For intInnerLoop = 0 To UBound(shtA)

bntRedDemo.Text = Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop))

Next intInnerLoop

End If

Next intOuterLoop

23 21

23 23

23 12

…12 21

12 23

12 12

intOuterLoop = 1

intOuterLoop = 6

{21, 23, 45, 12, 23, 65, 12}

21 21

21 23

21 12

intOuterLoop = 0

Dim blnWasRepeat As Boolean = False

For intOuterLoop = 0 To UBound(shtA)

For intInnerLoop = 0 To UBound(shtA)

If intOuterLoop <> intInnerLoop

If shtA(intOuterLoop) = shtA(intOuterLoop)

blnWasRepeat = True

End If

End If

Next intInnerLoop

Next intOuterLoop

Dim blnWasRepeat As Boolean = False

For intOuterLoop = 0 To UBound(shtA)

For intInnerLoop = 0 To UBound(shtA)

If intOuterLoop <> intInnerLoop

If shtA(intOuterLoop) = shtA(intOuterLoop)

blnWasRepeat = True

End If

End If

Next intInnerLoop

Next intOuterLoop

Recommended