Download presentation
Presentation is loading. Please wait.
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: 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.Arrays
2
Computer Memory shtGrades (short) Index0123 Values0000 Dim shtGrades(3) As Short
3
Computer Memory shtGrades (short) Index0123 Values21000 shtGrades(0) = 21 We can assign an individual variable by using its name, and index. Index and subscript are synonymous
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 < 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
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 < 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..
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 < 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…
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 < 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
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 > 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
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 >= 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?
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 >= 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
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 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} 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 < (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) 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 < (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…
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 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 21234512 …
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 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 2321 4512 …
17
21 21 2421 12 … intOuterLoop = 0 {21, 23, 45, 12, 23, 65, 12} intInnerLoop = 0 intInnerLoop = 1 intInnerLoop = 2 Str(shtA(intOuterLoop)) & " " & Str(shtA(intInnerLoop))
18
23 2123 23 12 … 12 2112 2312 … intOuterLoop = 1 intOuterLoop = 6 {21, 23, 45, 12, 23, 65, 12} … 21 21 2321 12 … intOuterLoop = 0
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 <> 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
20
23 2123 23 12 … 12 2112 2312 … intOuterLoop = 1 intOuterLoop = 6 {21, 23, 45, 12, 23, 65, 12} … 21 21 2321 12 … intOuterLoop = 0
21
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 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 Next intInnerLoop Next intOuterLoop
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.