180 likes | 200 Views
VB Arrays Chapter 8. Dr. John P. Abraham Professor UTPA. Arrays. Multiple elements of the same data type. Most languages the number of elements are static and defined at declaration In VB arrays can be re-dimensioned at execution time. Single dim, two dim and multiple dim.
E N D
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA
Arrays • Multiple elements of the same data type. • Most languages the number of elements are static and defined at declaration • In VB arrays can be re-dimensioned at execution time. • Single dim, two dim and multiple dim
Declaring an Array • Declaration (different ways) • Dim grades as integer() • Grades = New Integer(0 to 9) {} • {} initializer list • Grades = New Integer(9) {} • Dim grades() as Integer • Declare and Initialize • Dim grades as Integer()={90,80,88,90,70}
Referring to an Element • Name and position • Grades(6) = 85 • First element is 0th index (subscript) • Index must be a nonnegative integer • Index may have a calculated value like Grades (i+4*2) • Know the difference between index and value stored there • Sum = Grades(1) + Grades(2) + Grades(3)
Array is a Class • Class is in System.Array X = Grades.GetUpperBound(0) • Will give 9 (see the declaration) X= grades.GetLength(0) • Will give 10
Explain abstract structures • Card program explained • Two intermediate assignments explained • Month conversion • Standard deviation
List Boxes explained • List box • lstBoxMonths.Items.Add(monthNames(i)) • Check List Box • chkLstBoxMonths.Items.Add(monthNames(i)) • Combo Box with drop down • comboBoxMonths.Items.Add(monthNames(i))
Linear Search explained Dim searchMonth As String Dim found As Boolean searchMonth = txtMonthName.Text found = False : i = 1 While (Not (found)) And (i <= 12) If searchMonth = monthNames(i) Then found = True lblMonthNum.Text = i.ToString End If i += 1 End While
Splitting a string Dim grades() As String = Split(txtEnterGrade.Text, " ")
Passing Arrays Always as byVal (it is just a pointer to the 0th element) Declaration Dim scores(80) As Integer Function standardDeviation(ByVal scores() As Integer, ByVal numscores As Integer) As Double Call stdDev = standardDeviation(scores, numScores)
Manipulating array using loops For i = 0 to scores.GetUpperBound(0) Total += scores(i) Next
Card Game Two parts: text (string) and graphics. Realize you really do not move the string around, you work with the indices. For instance to display shuffled cards: For i = 1 to 52 Display OriginalDeck(shuffled(i)) Next
Sorting & Searching Array.Sort exists, but don’t use it. Sorting You need to implement it Don’t use the method sort comes with array class. You may use bubble sort or selection sort.
Bubble sort sorted = False While Not (sorted) sorted = True For i = 1 To j - 1 If CustomerName(i) > CustomerName(i + 1) Then Call swap(i) sorted = False End If Next i j = j - 1 Wend
Swap Private Sub swap(i) Dim tmp1 As String * 25 Dim tmp2 As String * 11 tmp1 = CustomerName(i) CustomerName(i) = CustomerName(i + 1) CustomerName(i + 1) = tmp1 tmp2 = CustomerTele(i) CustomerTele(i) = CustomerTele(i + 1) CustomerTele(i + 1) = tmp2 End Sub
Searching Linear search Binary search
Binary search Dim Lookfor As String * 25 Dim first, last, middle As Integer Dim nfound As Boolean LblName.Caption = "": LblTele.Caption = "" Lookfor = TxtSearch.Text nfound = False first = 1: last = Index While (Not (nfound) And first <= last) middle = (first + last) \ 2 If RTrim(Lookfor) = RTrim(CustomerName(middle)) Then nfound = True LblName.Caption = CustomerName(middle) LblTele.Caption = CustomerTele(middle) ElseIf Lookfor < CustomerName(middle) Then last = middle - 1 Else: first = middle + 1 End If Wend If Not (nfound) Then LblName.Caption = "Name not in list" TxtSearch.Text = ""
Using an array for a stack Keep track of top. Increase it before adding (push), Decrease it after taking (pop). Push or Pop only to or from the top. Examine the stack to see if it is empty or full For example, if stack.top < 0 it is empty. You may choose not to use the 0th location for data, then you can say it is empty when stack.top is zero.