Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Chapter 7 Arrays

2 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 0 X 0000 X(1) X(2) X(3) X(4) X

4 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) 0000 X(1) X(2) X(3) X(4) X

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

6 6 Array Declaration Syntax Dim arrayName(m To n) As varType where m and n are integers Declaring arrays - specify: –name –type of array –number of elements

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

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

9 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 0 0 0 0 0 Grade(1) Grade(2) Grade(3) Grade(4) Grade(5) 5 30 25 15 30 15 25

10 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 0 0 0 0 0 Grade(1) Grade(2) Grade(3) Grade(4) Grade(5) 1 2 3 4 5

11 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 0 0 0 0 0 Grade(1) Grade(2) Grade(3) Grade(4) Grade(5) 5 30 15 0 x 0 y 4 -5 4 22 17 30

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 0 0 0 0 0 Grade(1) Grade(2) Grade(3) Grade(4) Grade(5) 5 30 15 0 x 0 Sum 1 5 2 35 3 4 50 5 6

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 0000 x(0) x(1)x(2)x(3) 0000 x(0) x(1)x(2)x(3)

14 Example Dim Y(-1 to 2) As Integer 0000 Y(-1) Y(0)Y(1)Y(2) Dim Z(-2 to 0) As Single 000 Z(-2) Z(-1)Z(0)

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

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

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

18 500500

19

20 m, n must be a constant

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

22 Chapter 7 - Visual Basic Schneider22 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 AliAhmed Huda Student 1230 22 19 25 Score 1230 Ali 22 Ahmed 19.. Huda 25 Students.txt

23 23 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 If all entries in the file are read, the EOF returns True When the file is opened, the EOF returns False until all entries in the file are read

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

25 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

26 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 Schneider26

27 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

28 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

29 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

30 30 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. 40301015 x(0) x(1)x(2)x(3)

31 31 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

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

33 33

34 34

35

36

37 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 Swapping 2 elements in an array 5 2 x1 x2 X3 = x2 X2 = x1 X1 = x3 5 x3

38 38 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

39 Bubble Sort Sorted

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

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

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 5 12 3542 77 101 1 2 3 4 5 6

43 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 5 12 3542 77 101 1 2 3 4 5 6 Swap 4277

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 5 12 3577 42 101 1 2 3 4 5 6 Swap 3577

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 5 12 7735 42 101 1 2 3 4 5 6 Swap 1277

46 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 5 77 1235 42 101 1 2 3 4 5 6 No need to swap

47 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 5 77 1235 42 101 1 2 3 4 5 6 Swap 5101

48 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 77 1235 42 5 1 2 3 4 5 6 101 Largest value correctly placed

49 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 42 3512 5 77 1 2 3 4 5 6 101 Largest value correctly placed

50 50 Bubble Sort (array size = 5) For passNum = 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

51 51 Swapping two variables Private Sub SwapData (A() As String, index As Integer) Dim temp As String temp = A(index) A(index) = A(index + 1) A(index + 1) = temp End Sub

52 7 0 5 2 4 7 0 2 5 4 7 0 2 5 4 4 0 2 5 7 Pass 1 4 0 2 5 7 4 0 2 5 7 5 0 2 4 4 Pass 2 77 After the first pass, the last item (7) will be in its proper position

53 5 0 2 4 7 5 0 2 4 7 Pass 3

54 2 0 10 5 2 0 5 10 10 0 5 2 0 5 2 10 Pass 1 0 5 2 10 0 2 5 7 5 0 2 4 Pass 2 10 After the first pass, the last item (7) will be in its proper position

55 5 0 2 10 5 0 2 10 Pass 3 The number of passes = the number of elements in the array – 1. # of passes to sort array with 4 elements = (n-1) = 4 - 1 = 3 The number of comparisons = n(n-1)/2 = 4*3/2 = 6 comparisons

56 56 Bubble Sort The number of passes used to sort the elements in an array is equal to the number of elements in the array less 1. Passes = Array size - 1 Bubble sort works well for sorting small arrays, but is often too slow for very large ones.

57 57 Shell Sort Similar to the bubble sort Instead of comparing and swapping adjacent elements A(count) and A(count+1), Shell sort compares and swaps non-adjacent elements A(count) and A(count + Gap), where Gap starts at roughly half the size of the array

58 58 Shell Sort At the end of each pass, if no elements have been swapped in the current pass, then Gap is halved for the next pass. Eventually, Gap becomes one, and adjacent elements are compared and swapped as necessary.

59 978653100 Gap = 8/2 = 4 numOfparts = 8 12345678 Gap 978653100 Swap 578693100 Swap

60 538697100 Swap Gap = 8/2 = 4 numOfparts = 8 End of Pass 1: doneFlag = false 538097106 538697 0 No Swap

61 538097106 No Swap 538097106 No Swap 538097106 No Swap 538097106 End of Pass 3 : doneFlag = true Gap = gap/2 = 4/2 = 2

62 No Swap 538097106 Gap 538097106 Swap 538097106 No Swap 508397106 No Swap 508397106

63 No Swap 508397106 No Swap 508397106 508396 7 End of Pass 1 : doneFlag = false 508396107 No Swap 508396107 Swap

64 No Swap 508396107 No Swap 508396107 No Swap 508396107 No Swap 508396107 End of Pass 2 : doneFlag = true Gap = gap/2 = 2/2 = 1

65 Swap 508396107 No Swap 058396107 Swap 058396107 No Swap 053896107 Swap 053896107

66 No Swap 053869107 Swap 053869107 0538697 End of Pass 1 : doneFlag = false 053869710 No Swap 053869710 Swap

67 035869710 Swap 035869710 No Swap 035869710 Swap 035689710 No Swap 035689710 Swap

68 035687910 No Swap End of Pass 2 : doneFlag = false 035687910 No Swap 035687910 No Swap 035687910 No Swap

69 035687910 No Swap 035687910 Swap 035678910 No Swap 035678910 No Swap End of Pass 3 : doneFlag = false

70 035678910 No Swap 035678910 No Swap 035678910 No Swap 035678910 No Swap 035678910 No Swap

71 035678910 No Swap 035678910 No Swap End of Pass 4 : doneFlag = true Gap = gap/2 = 1/2 = 0 Don’t forget: Do While gap >= 1 DONE: Array sorted with SHELL SORT

72 (X)Shell Sort (X) This Code is Not Included in Exams Dim gap As Integer Dim doneFlag As Boolean Dim numParts As Integer ' 8 Dim part(1 To 8) gap = Int(numParts / 2) ' 4 Do While gap >= 1 Do 'loop until no elements have been swapped in the pass doneFlag = True For Index = 1 To numParts - gap If part(Index) > part(Index + gap) Then Call Swap(part(index), part(index + gap) doneFlag = False End If Next Index Loop Until doneFlag = True gap = Int(gap / 2) 'Halve the length of the gap Loop

73 73 Efficiency of Bubble and Shell sort (average number of comparisons) Array Elements Bubble Sort Shell Sort 51015 15105115 25300302 30435364 1004,9502,638 500124,75022,517

74 74 Searching Arrays The process of finding the position of a value in an array is called searching For example Search(-10) = 3 Search (3) = 1 Search (5) = Not Found -1036 stdID(1) stdID(2)stdID(3)

75 75 Searching techniques A sequential search examines each element, beginning with the first, until the specified value is found or the end of the array is reached For example Search(4) = 5 -1037 stdID(1) stdID(2)stdID(3) 1014 stdID(4) stdID(5)stdID(6) Is 4 = 3: False Is 4 = 7: False Is 4 = -10: False Is 4 = 1: False Is 4 = 4: True The number of comparisons to find the value 4 = 5

76 76 Example of a Sequential Search Dim nom(1 To 6) As Integer Dim index As Integer, foundFlag As Boolean Do While (index <= 6) And (Not foundFlag) If nom(index)= 4 Then foundFlag = True Else index = index + 1 End If Loop picOutput.Print “The Number 4 is located at position“; index

77 77 Sequential Search Useful for small arrays. Very inefficient for large arrays (for example, names in a telephone book). For any size array, if the array is ordered, the more efficient binary search can be used.

78 78 Binary Search In a binary search, an ordered array is repeatedly divided in half. The half not containing the target value is ignored.

79 82134657109111214130 64141325335143538472939597966 Binary Search lo Binary search. Given value and sorted array a[] Search About number 33 hi

80 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lo hi mid

81 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lo hi

82 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lo midhi

83 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lohi

84 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lohimid

85 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lo hi

86 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lo hi mid

87 Binary Search Binary search. Given value and sorted array a[] Search About number 33 82134657109111214130 64141325335143538472939597966 lo hi mid

88 Private Sub BinarySearch(corp As String, result As String) Dim foundFlag As Boolean, first as Integer, last as Integer first = 1 last = numFirms Do While (first <= last) And (foundFlag = False) middle = Int((first + last) / 2) Select Case UCase(firm(middle)) Case corp foundFlag = True Case Is > corp last = middle - 1 Case Is < corp first = middle + 1 End Select Loop ‘continued on next slide If foundFlag = True Then result = "found” Else result = "not found End If End Sub 88 Binary Search This Code is Not Included in Exams

89 8 3 7 stdID(1) stdID(2) stdID(3) 77 10 22 stdID(4) stdID(5) stdID(6) 81 78 80 100 91 92 stdID(7) stdID(8) stdID(9) stdID(10) stdID(11) stdID(12) Search(78) = First = 1 Last = 12 Middle = Int((first + last) / 2) Middle = Int(1+12)/2=6 Middle=6 Is 78 = 77  false Is 78< 77  false Is 78>77  First =7 First = 7 Middle = Int(7+12)/2=9 Middle= 9 Is 78 = 81  false Is 78 < 81  Last = 8 Last = 8 Middle = Int(7+8)/2=7 Middle = 7 Is 78 = 78  true STOP Search(78) =7

90 90 Two-Dimensional Arrays Store values as a table, grouped into rows and columns. The first subscript of a two-dimensional array refers to the row and the second subscript to the column.

91 91 Declaration of Two-Dimensional Array Syntax: –Dim arrayName(m1 To m2, n1 To n2) As varType Example: – Dim rm(1 To 4, 1 To 4) As Single column row

92 Private Sub Command1_Click() Dim ar(1 To 5, 1 To 6) As Integer Dim r As Integer, c As Integer For r = 1 To 5 For c = 1 To 6 ar(r, c) = r * c Next c Next r For r = 1 To 5 For c = 1 To 6 Print ar(r, c), Next c Print Next r End Sub Example

93 93 Manipulating a Two-Dimensional Array Use nested For … Next loops to assign or access elements of a two-dimensional array. Example: For row = 1 To 4 For col = 1 To 4 Input #1, rm(row, col) Next col Next row

94 What is the output of: Dim (1 to 3, 1 to 3) as integer For i = 1 To 3 For j = 1 To 3 a(i, j) = i * j Print a(i, j); Next j Print Next i The output is 1 2 3 2 4 6 3 6 9

95 Examples: How many elements? Dim rm(1 To 4) As Single 95 000 rm(1) rm(2)rm(3) 0 rm(4)  4 elements

96 Examples: How many elements? Dim rm(0 To 3, 97 To 99) As Integer 96 000 000 000 000 999798 0 1 2 3  12 elements

97 Examples: How many elements? Dim arr(4, 1 To 4) As Single 97 000 000 000 000 312 0 1 2 3 0 0 0 0 4 0000 4  20 elements

98 examples Dim a(5) as integer –6 elements from 0 to 5 Dim a(4,4) as string –25 elements Dim a(5 to 1) as integer –Error Dim a(-5) as integer –Error Dim a(0) as integer –1 element (from 0 to 0)

99 Examples: How many elements? Dim arr(-5 To -3, 5 To 5) As String 99 5 -5 -4 -3  3 elements

100 Examples: How many elements? Dim xy(-3 To -5) As Integer 100  ERROR

101 Examples Dim arr(0 To 3, 97 To 99) As Integer –arr(1,98)= 20 –arr(3,99)=15 –arr(0,2)  Error: index out of range –arr(-1,99)  Error: index out of range 101 000 0020 000 1500 999798 0 1 2 3

102 Examples Dim arr(0 To 3, 97 To 99) As Integer For i = 97 To 98 arr(1,i) = 10 Next i 102 000 10 000 000 999798 0 1 2 3

103 Examples: How many elements? Dim rm(1 To 4) As Single4 Dim rm(4, 1 To 4) As Single20 Dim rm(1 To 4, 1 To 4) As Single16 Dim rm(0 To 3, 97 To 100)16 Dim rm (-5 To -3, 5 To 5)3 Dim rm(-3 To -5)Error Chapter 7 - Visual Basic Schneider103

104 Reference Chapter 7 - Visual Basic Schneider


Download ppt "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."

Similar presentations


Ads by Google