1 / 18

VB Arrays Chapter 8

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.

vrodarte
Download Presentation

VB Arrays Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA

  2. 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

  3. 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}

  4. 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)

  5. 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

  6. Explain abstract structures • Card program explained • Two intermediate assignments explained • Month conversion • Standard deviation

  7. 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))

  8. 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

  9. Splitting a string Dim grades() As String = Split(txtEnterGrade.Text, " ")

  10. 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)

  11. Manipulating array using loops For i = 0 to scores.GetUpperBound(0) Total += scores(i) Next

  12. 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

  13. 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.

  14. 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

  15. 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

  16. Searching Linear search Binary search

  17. 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 = ""

  18. 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.

More Related