Dim ArrayName(UpperBound) As Datatype

About This Presentation
Title:

Dim ArrayName(UpperBound) As Datatype

Description:

Dim ArrayName(UpperBound) As Datatype. An array is declared using the following syntax: ... obeys the same rules as when declaring a variable: it begins with a ... –

Number of Views:30
Avg rating:3.0/5.0
Slides: 23
Provided by: non882
Learn more at: http://www.cs.ucr.edu
Category:

less

Transcript and Presenter's Notes

Title: Dim ArrayName(UpperBound) As Datatype


1
Arrays
An array is declared using the following syntax
Dim ArrayName(UpperBound) As Datatype
An arrays 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.
2
Dim shtGrades(3) As Short
shtGrades (short) shtGrades (short) shtGrades (short) shtGrades (short) shtGrades (short)
Index 0 1 2 3
Values 0 0 0 0
Computer Memory
3
shtGrades(0) 21
We can assign an individual variable by using
its name, and index. Index and subscript are
synonymous
shtGrades (short) shtGrades (short) shtGrades (short) shtGrades (short) shtGrades (short)
Index 0 1 2 3
Values 21 0 0 0
Computer Memory
4
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 lt
shtAges(intLoopCount) Then
intMaxVal shtAges(intLoopCount) End
If Next intLoopCount bntRedDemo.Text
intMaxVal.ToString
Finding the largest value in an array
5
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 lt
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..
6
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 lt 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
7
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 lt 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
8
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 gt shtA(intLoopCount) Then
intMinVal shtA(intLoopCount)
intLocation intLoopCount End If
Next intLoopCount bntRedDemo.Text Value
str(intMinVal ) , at str(intLocation)
Note pessimistic assumption
Note relational test
Consider finding the minimum value, again a
pessimistic assumption
9
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 gt 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?
10
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 gt shtA(intLoopCount) Then
intMinVal shtA(intLoopCount)
intLocation intLoopCount End If
Next intLoopCount bntRedDemo.Text Value
str(intMinVal ) , at str(intLocation)
From now on I wont bother showing this stuff,
you can assume it from context
11
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
21, 23, 45, 12, 23, 65, 12
If IntFindMe was say, 1111
Note the pessimistic assumption. Question Does
it find the first or last occurrence?
12
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 lt (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)
Looks good, but there is a bug!
13
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 lt (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
14
Let us try to find a repeated item
15
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

16
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

17
intOuterLoop 0
21, 23, 45, 12, 23, 65, 12
intInnerLoop 0
intInnerLoop 1

intInnerLoop 2
Str(shtA(intOuterLoop)) " "
Str(shtA(intInnerLoop))
18
intOuterLoop 0
21, 23, 45, 12, 23, 65, 12
intOuterLoop 1

intOuterLoop 6



19
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 ltgt intInnerLoop For
intInnerLoop 0 To UBound(shtA)
bntRedDemo.Text Str(shtA(intOuterLoop)) " "
Str(shtA(intInnerLoop)) Next intInnerLoop
End If Next intOuterLoop
20
intOuterLoop 0
21, 23, 45, 12, 23, 65, 12
intOuterLoop 1

intOuterLoop 6



21
Dim blnWasRepeat As Boolean False For
intOuterLoop 0 To UBound(shtA) For
intInnerLoop 0 To UBound(shtA) If
intOuterLoop ltgt intInnerLoop If
shtA(intOuterLoop) shtA(intOuterLoop)
blnWasRepeat True End If
End If Next intInnerLoop Next intOuterLoop
22
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com