1.04k likes | 1.06k Views
Learn about one-dimensional and two-dimensional arrays, searching and sorting arrays, and the difference between arrays and simple variables.
E N D
Chapter 7 Arrays
Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching Learn about sorting
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
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
Array Grade() Dim Grade( 1 To 4) As Integer Array Name 35 10 5 25 Grade(1) Index 5
Array Declaration SyntaxDimarrayName(mTon) AsvarTypewhere m and n are integers Declaring arrays - specify: name type of array number of elements
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
The Dim Statement Used to declare an array A Dimstatement must occur before the first reference to the array elements.
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
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
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
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)
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
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
Example • Dim Y(-1 to -1) As Integer Y(-1) 0 • Dim Z(2 to 2) As Single Z(2) 0
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
Example • Dim Y(10 to 1) As Integer • Compile ERROR • Dim Z(2 to -2) As Single • Compile ERROR
5 0 0
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
Sorted Bubble Sort
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.
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.
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
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
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
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
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
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
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
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
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