1 / 104

Introduction to Arrays in Visual Basic

Learn about one-dimensional and two-dimensional arrays, searching and sorting arrays, and the difference between arrays and simple variables.

cshelby
Download Presentation

Introduction to Arrays in Visual Basic

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. Chapter 7 Arrays

  2. Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching Learn about sorting

  3. Array verses Simple Variable Simple variable is used to store a single value. Ex: Dim X as Integer Array variable is used to represent many values of the same type with one variable name. Ex: Dim X(1 to 4) as Integer X(2) X(4) X(1) X(3) 0 0 0 0 X X 0

  4. Elements of an Array Array Name: A valid variable name for the structure. (X) Subscript or Index : A value that refers to a particular array element. X(1) Element or Subscripted variable: An individual data item within an array. Ex: X(1), X(2) X(2) X(4) X(1) X(3) 0 0 0 0 X

  5. Array Grade() Dim Grade( 1 To 4) As Integer Array Name 35 10 5 25 Grade(1) Index 5

  6. Array Declaration SyntaxDimarrayName(mTon) AsvarTypewhere m and n are integers Declaring arrays - specify: name type of array number of elements

  7. Array Declaration Examples Dim month(1 To 5) As String Dim stdID(1 To 3) As Integer Month(2) Month(3) Month(4) Month(5) Month(1) This statement creates array of 3 integer elements stdID(2) stdID(3) stdID(1) 0 0 0 7

  8. The Dim Statement Used to declare an array A Dimstatement must occur before the first reference to the array elements.

  9. Initializing an Array Private Sub cmdExample_Click() Dim Grade(1 To 5) As Integer Grade (1) = 5 Grade (2) = 30 Grade (3) = 25 Grade (4) = 15 Print Grade(1) + Grade(3) Print Grade(4) Print Grade(1+2) End Sub Grade(1) 5 0 Grade(2) 0 30 0 25 Grade(3) 0 15 Grade(4) 0 Grade(5) 30 15 25

  10. Initializing an Array Private Sub cmdExample_Click() Dim Grade(1 To 5) As Integer For i = 1 To 5 Step 1 Grade(i)=i Next i End Sub Grade(1) 0 1 Grade(2) 0 2 0 3 Grade(3) 0 4 Grade(4) 5 0 Grade(5) 10

  11. Private Sub cmdExample_Click() Dim x as Integer, y as Integer Dim Grade(1 To 5) As Integer Grade (1) = 5 Grade (2) = 30 Grade (4) = 15 x = 4 y= -5 Grade(1) = x Grade(x+1) = 22 Grade(3) = (Grade(1)+Grade(2))/2 y = Grade(2) Print Grade(1+2) End Sub x 4 0 y 30 -5 0 Grade(1) 4 5 0 Grade(2) 30 0 17 0 Grade(3) 15 0 Grade(4) 0 22 Grade(5) 11

  12. Private Sub cmdExample_Click() Dim x as Integer, Sum as Integer Dim Grade(1 To 5) As Integer Grade (1) = 5 Grade (2) = 30 Grade (4) = 15 x = 1 Do While x<=5 Sum=Sum + Grade(x) x = x + 1 Loop Print “Sum = “;Sum End Sub x 5 4 3 2 1 0 6 Sum 5 50 0 35 50 35 Grade(1) 5 0 Grade(2) 0 30 0 Grade(3) 0 15 Grade(4) 0 Grade(5)

  13. The statements Dim arrayName(0 to n) as VarType can be placed by the statement Dim arrayName(n) as VarType • Ex. Dim X(0 To 3) As Integer • Ex. Dim X(3) As Integer x(1) x(2) x(3) x(0) 0 0 0 0 x(1) x(2) x(3) x(0) 0 0 0 0

  14. Example • Dim Y(-1 to 2) As Integer Y(0) Y(1) Y(2) Y(-1) 0 0 0 0 • Dim Z(-2 to 0) As Single Z(-1) Z(0) Z(-2) 0 0 0

  15. Example • Dim Y(-1 to -1) As Integer Y(-1) 0 • Dim Z(2 to 2) As Single Z(2) 0

  16. Example • Dim Y(2) As Integer Y(0) Y(1) Y(2) 0 0 0 • Dim Z(2 to 2) As Single Z(2) 0

  17. Example • Dim Y(10 to 1) As Integer • Compile ERROR • Dim Z(2 to -2) As Single • Compile ERROR

  18. 5 0 0

  19. m, n must be a constant

  20. Dim XY (1 To 5) As Integer Dim count As Integer Open “DATA.TXT” For Input As #1 For count = 1 To 5 Input #1, XY(count) Next count The input statements is used to look for the next available item of data and assign it to the variable XY(count) 21

  21. Adding Up Elements in an Array Dim score(1 To 30) As Single, student(1 To 30) As String Dim count As Integer, average as Integer, sum As Integer Open “STUDENT.TXT” For Input As #1 For count = 1 To 30 Input #1, student(count), score(count) Next count sum = 0 For count = 1 To 30 sum = sum + score(count) Next count average = sum/30 Students.txt Ali 22 Ahmed 19 .. .. .. Huda 25 1 2 30 Student Ali Ahmed Huda 1 2 30 Score 19 22 25 Chapter 7 - Visual Basic Schneider 22

  22. Dim XY (1 To 5) As Integer Dim count As Integer count = 1 Open “DATA.TXT” For Input As #1 Do While NOT EOF(1) Input #1, XY(count) count= count + 1 Loop will be true if the end of file has been reached, and false otherwise When the file is opened, the EOF returns False until all entries in the file are read If all entries in the file are read, the EOF returns True

  23. Parallel Arrays Two arrays are said to be parallel if subscripted variables having the same subscript are related. Students.txt Ali 22 Ahmed 19 .. .. .. Huda 25 1 2 30 Student Ali Ahmed Huda 1 2 30 Score 19 24 22 25

  24. Example of Parallel Arrays Dim nom(1 To 3) As String, score(1 To 3) As Integer Dim student As Integer Open “SCORE.TXT” For Input As #1 For student = 1 To 3 Input #1, nom(student), score(student) Next student Close #1

  25. Form_Load() Use it to Initialize Form Level variables Use it to initialize Form Level Arrays Use it to Dim Dynamic Arrays Chapter 7 - Visual Basic Schneider 26

  26. Dynamic Arrays • Defines or dimensions a Dynamic array • ReDim arrayName (m To n) as varType • m, n can be a variables or expressions • ReDim can only be used inside a procedure • Use Dim in General Declaration as • Dim arrayName() as varType • Cannot be used till you ReDim it in a procedure

  27. About ReDim: • Dim A() as integer • Redim A(7) as integer  Valid • Dim A() as integer • X=1 • ReDim A(x) as integer  Valid • Dim A() as integer • A(1)= 5  Invalid

  28. About ReDim: • Dim A(5) as integer • ReDim A(7)  Invalid • Redim A(5) • Dim A(7)  Invalid • ReDim A(5) • ReDim A(7) as integer  Invalid

  29. Ordered Array An array is ordered if its values are in either ascending or descending order. For string arrays, the ANSI table is used to evaluate the “less than or equal to” condition. x(1) x(2) x(3) x(0) 10 15 30 40

  30. Passing an Array An array can be passed to another procedure (Only) by reference. the name of the array, followed by an empty set of parenthesis, must appear as an argument in calling statement, and an array variable name of the same type must appear as corresponding parameters in the procedure definition of the procedure that is to receiver the array Parenthesis are optional with the arguments

  31. Since the array score is passed by reference, the array values (s(1), s(2), and s(3)) are sent back to the array score.

  32. 33

  33. Swapping 2 elements in an array Private Sub Command1_Click() Dim arr(3 To 7) As Integer arr(3) = 10 arr(4) = 3 arr(5) = 2 arr(6) = 9 arr(7) = 88 Print arr(3), arr(7) Call swap(arr()) Print arr(3), arr(7) End Sub Private Sub swap(a() As Integer) Dim temp As Integer temp = a(3) a(3) = a(7) a(7) = temp End Sub 5 x1 2 5 x2 x3 X3 = x2 X2 = x1 X1 = x3

  34. Sorting A common practice involving arrays is to sort the elements of the array in either ascending or descending order. A sort is an algorithm for ordering an array Sorting techniques Bubble Sort Shell Sort Ascending : lowest to highest descending: highest to lowest

  35. Sorted Bubble Sort

  36. Bubble Sort The bubble sort involves comparing adjacent elements and swapping the values of those elements when they are out of order. One complete time through an array is called a pass.

  37. Bubble Sort • The bubble sort involves comparing adjacent elements and swapping the values of those elements when they are out of order. • One complete time through an array is called a pass.

  38. bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 12 42 35 5 77

  39. 42 77 bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 1 2 3 4 5 6 101 12 42 35 5 77

  40. 35 77 bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 77 35 5 42

  41. 12 77 bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 35 77 5 42

  42. bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 77 35 12 5 42 No need to swap

  43. 5 101 bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 77 35 12 5 42

  44. bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 5 77 35 12 42 Largest value correctly placed

  45. bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 77 42 12 35 5 Largest value correctly placed

  46. Bubble Sort (array size = 5) ForpassNum = 1 To 4 For index = 1 To 5 - passNum If name(index) > name(index + 1) Then Call SwapData(name(), index) End If Next index Next passNum

More Related