Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s...
-
date post
21-Dec-2015 -
Category
Documents
-
view
221 -
download
1
Transcript of Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s...
![Page 1: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/1.jpg)
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
![Page 2: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/2.jpg)
Computer Memory
shtGrades (short)Index 0 1 2 3
Values 0 0 0 0
Dim shtGrades(3) As ShortDim shtGrades(3) As Short
![Page 3: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/3.jpg)
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
![Page 4: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/4.jpg)
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
![Page 5: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/5.jpg)
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..
![Page 6: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/6.jpg)
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…
![Page 7: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/7.jpg)
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
![Page 8: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/8.jpg)
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
![Page 9: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/9.jpg)
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?
![Page 10: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/10.jpg)
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
![Page 11: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/11.jpg)
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?
![Page 12: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/12.jpg)
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!
![Page 13: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/13.jpg)
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…
![Page 14: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/14.jpg)
Let us try to find a repeated item
![Page 15: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/15.jpg)
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…
![Page 16: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/16.jpg)
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…
![Page 17: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/17.jpg)
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))
![Page 18: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/18.jpg)
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
![Page 19: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/19.jpg)
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
![Page 20: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/20.jpg)
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
![Page 21: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/21.jpg)
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
![Page 22: Dim 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:](https://reader030.fdocuments.net/reader030/viewer/2022032521/56649d585503460f94a37b77/html5/thumbnails/22.jpg)