Download presentation
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 Learn about sorting
3
Array verses Simple Variable
Simple variable is used to store a single value. Ex: Dim X as Integer X Array variable is used to represent many values of the same type with one variable name. Ex: Dim X(1 to 4) as Integer Suppose you want to evaluate the exam grades for 30 students.,this means you have to create 30 string variables for names and 30 single vvariables for their scores Array holds either all string values or all numeric values. X(1) X(2) X(3) X(4) X
4
Elements of an Array X(1) X(2) X(3) X(4) X 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) The elements of an array are assigned successive memory locations. The dim statement creates an array of 5 elements of type integer with a default value = 0
5
35 10 5 25 Dim Grade( 1 To 4) As Integer Array Grade() Grade(1)
Array Name The elements of an array are assigned successive memory location. Grade(1) Index 5 5
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 Arrayname follows the same rules as variable names. Each array has sufffiient memory reserved to hold the number of items of the given type..
7
This statement creates array of 3 integer elements
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) Arrayname follows the same rules as variable names. Each array has sufffiient memory reserved to hold the number of items of the given type.. This statement creates array of 3 integer elements stdID(1) stdID(2) stdID(3) 7 7
8
The Dim Statement Used to declare an array
A Dim statement must occur before the first reference to the array elements. You can declare arrays in the declaration section of general , which are form level variable or pass them as arguments to different sub program
9
Initializing an Array Private Sub cmdExample_Click()
Grade(1) 5 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(2) 30 25 Grade(3) 15 Grade(4) Grade(5) 30 15 25
10
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(0) x(1) x(2) x(3) x(0) x(1) x(2) x(3)
11
Example Dim Y(-1 to 2) As Integer Dim Z(-2 to 0) As Single Y(-1) Y(0)
Dim Z(-2 to 0) As Single Z(-2) Z(-1) Z(0)
12
Example Dim Y(-1 to -1) As Integer Y(-1) Dim Z(2 to 2) As Single Z(2)
13
Example Dim Y(2) As Integer Dim Z(2 to 2) As Single Y(0) Y(1) Y(2)
Dim Z(2 to 2) As Single Z(2)
14
Compile ERROR Compile ERROR Example Dim Z(2 to -2) As Single
Dim Y(10 to 1) As Integer Compile ERROR Dim Z(2 to -2) As Single Compile ERROR
15
Example i = 5 Dim arr(i) As Integer Invalid statements
16
Example 5
17
Example
18
Example m, n must be a constant
19
Initializing an Array by Reading from a File
STUDENTS.txt Dim student (1 To 30) As String Dim count As Integer Open “STUDENTS.txt” For Input As #1 For count = 1 To 30 Input #1, student(count) Next count Ali Ahmed .. Huda 1 2 30 Student Ali Ahmed Huda 19 Chapter 7 - Visual Basic Schneider 19
20
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 sum = sum + score(count) average = sum/30 Students.txt Ali 22 Ahmed 19 .. Huda 1 2 30 Student Ali Ahmed Huda 1 2 30 Score 19 22 25 Chapter 7 - Visual Basic Schneider 20 20
21
Example 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 The loop will repeated as long as the end of file is not reached
22
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 1 2 30 Student Ali Ahmed Huda 1 2 30 Score 19 22 25
23
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
24
Chapter 7 - Visual Basic Schneider
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 24
25
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(0) x(1) x(2) x(3) 10 15 30 40
26
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
27
Example 27 27
28
Example
29
Example
30
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 Ascending : lowest to highest descending: highest to lowest When characters are compared the computer uses the ASCII value of each character to compare if the letter A is less than the letter B
31
Bubble Sort The bubble sort involves comparing adjacent elements and swapping the values of those elements when they are out of order. Notice that the Flag is set to 1 before the loop is entered . Its value is later checked to determine if the entire array is in order. The Final is set to one less than the number of items to be sorted. This is because two items at a time are compared . And you do not want to go over the boundary of the array Examine the block IF statement in the FOR… NEXT loop . This statement determines whether two adjacent element should beexchanged. The following program segment demonstrate SWAP X = 20 Y = 10 SWAP X, Y We need a DO WHILE… LOOP to sort the whole list. As long as FLAG =1 Basic knows switches have been made When the loop is completed without setting the Flag = 1 - that is when no switches are made
32
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 Gap is equal to one-half the size of the list. The elements of the list are separated by the chosen gap and grouped into subsists. Is more efficient for longer lists (100 or more elements) Is a good compromise between speed and simplicity. The shell sort was named for its inventor, Donald Shell, is much more efficient for really long lists For very short list s, the bubble sort is preferable; however for a lis of 30 items or more, the shell sort will consistently outperform the bubble sort.
33
Efficiency of Bubble and Shell sort (average number of comparisons)
Array Elements Bubble Sort Shell Sort , ,638 , ,517
34
Searching Arrays The process of finding the position of a value in an array is called searching For example Note: search(x) means search about x Search(-10) = 3 Search (3) = 1 Search (5) = Not Found stdID(1) stdID(2) stdID(3) 3 6 -10
35
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 about number 4 Search(4) = 5 stdID(1) stdID(2) stdID(3) stdID(4) stdID(5) stdID(6) 3 7 -10 1 4 10 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 35 35
36
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.
37
Binary Search In a binary search, an ordered array is repeatedly divided in half. The half not containing the target value is ignored.
38
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.
39
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
40
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
41
Examples: How many elements?
Dim rm(0 To 3, 97 To 99) As Integer 97 98 99 1 2 3 12 elements 41
42
1 2 3 2 4 6 3 6 9 What is the output of: The output is
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
43
Examples: How many elements?
Dim rm(1 To 4) As Single rm(1) rm(2) rm(3) rm(4) 4 elements
44
Examples: How many elements?
Dim arr(4, 1 To 4) As Single 1 2 3 4 1 2 3 4 20 elements 44
45
Examples: How many elements?
Dim arr(-5 To -3, 5 To 5) As String 5 -5 -4 -3 3 elements 45
46
Examples: How many elements?
Dim xy(-3 To -5) As Integer ERROR 46
47
Examples Dim arr(0 To 3, 97 To 99) As Integer arr(1,98)= 20
arr(0,2) Error: index out of range arr(-1,99) Error: index out of range 97 98 99 1 20 2 3 15 47
48
Examples Dim arr(0 To 3, 97 To 99) As Integer For i = 97 To 99 Next i
arr(1,i) = 10 Next i 97 98 99 1 10 10 10 2 3 48
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.