Chapter 7 Arrays Outline and Objective - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Chapter 7 Arrays Outline and Objective

Description:

teamName(4) = 'Cubs' teamName(5) = 'Cubs' n = Val(txtNumber.Text) ... Chicago. Los Angeles. New York. Philadelphia. rm (2,2) rm (3,4) Section 7.5 Example 1 ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 38
Provided by: fay85
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Arrays Outline and Objective


1
Chapter 7 ArraysOutline and Objective
  • 7.1 Creating and Accessing Arrays
  • 7.2 Using Arrays
  • 7.3 Control Arrays
  • 7.4 Sorting and Searching
  • 7.5 Two-dimensional arrays

2
7.1 Array verses Simple Variable
  • Simple variable is used to store a single value.
  • Array variable is used to represent many values
    of the same type with one variable name.

3
Elements of an Array
  • Array Name A valid variable name for the
    structure.
  • Subscript or Index A value that refers to a
    particular array element.
  • Element An individual data item within an array.
  • Syntax
  • Dim arrayname(1 To n) As VarType

4
Examples of arrays
  • Dim month(1 To 12) As String
  • Dim score(1 To 30) As Single
  • Dim students(1 To 30) As String
  • The Dim Statement
  • Used when you need to define an array.
  • The Dim statement sets up storage for array.
  • A Dim statement must occur before the first
    reference to the array elements.

5
Initializing an Array See Section 7.1 pp. 314
Example 1
  • Private Sub cmdWhoWon_Click()
  • Dim teamName(1 To 5) As String
  • Dim n As Integer
  • ' Fill array with World Series Winners
  • teamName(1) "Red Sox"
  • teamName(2) "Giants"
  • teamName(3) "White Sox"
  • teamName(4) "Cubs"
  • teamName(5) "Cubs"
  • n Val(txtNumber.Text)
  • picWinner.Print "The " teamName(n) " won
    World Series number" n
  • End Sub

6
Array teamName()
Array Name
  • teamName(1 To 5) As String

Red Sox Giants White Sox Cubs Cubs
teamName(1)
Index
7
Initializing an Array by Reading from a File
  • Dim student(1 To 30) As String
  • Open STUDENTS.TXT For Input As 1
  • For count 1 To 30
  • Input 1, student(count)
  • Next count

8
Parallel Arrays
  • Two arrays are referred to as parallel if
    subscripted variables having the same subscript
    are related.
  • Example
  • Dim nom(1 To 8) As String , score(1 To 8) As
    Integer
  • Open SCORE.TXT For Input As 1
  • For student 1 To 8
  • Input 1, nom(student), score(student)
  • Next student
  • Close 1

9
Adding Rows in an Array
  • Dim total as Integer, student as Integer, average
    as Single
  • Dim nom(1 To 8) As String, score(1 To 8) As
    Integer
  • Open SCORES.TXT For Input As 1
  • For student 1 To 8
  • Input 1, nom(student), score(student)
  • Next student
  • Close 1
  • total 0
  • For student 1 To 8
  • total total score(student)
  • Next student
  • average total/8
  • picTopStudents.Cls
  • For student 1 To 8
  • If score(student) gt average Then
  • picTopStudents.Print nom(student)
  • End If
  • Next student
  • End Sub

See Example 3, page 317
10
7.2 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.
  • Example 1, pp. 333 - a program that places an
    ordered list of names into an array, requests a
    name as input, and informs the user if the name
    is in the list. See Figure 7.5 Flowchart.

11
Merging Two Ordered Arrays
  • Merge Algorithm to consolidate two ordered lists
    to create the 3rd list.
  • Compare the two names at the top of the 1st and
    2nd lists.
  • If one name alphabetically precedes the other,
    copy it onto the 3rd list and cross it off its
    original list.
  • If the names are the same, copy the name onto the
    3rd list and cross out the name from both lists.
  • Repeat Step 1 with the current top names until
    you reach the end of either list.
  • Copy the names from the remaining list onto the
    3rd list.

12
Passing Arrays Between Procedures
  • An array can be passed to another procedure by
    reference.
  • The name of the array, followed by an empty set
    of parentheses, must appear as an argument in the
    calling statement, and an array variable name of
    the same type must appear as a corresponding
    parameter in the procedure definition.
  • Private Sub cmddisplay_Click()
  • ' Pass array to subprogram and function
  • Dim score(1 To 5) As Integer
  • Call FillArray(score())
  • picAverage.Cls
  • picAverage.Print Average is" Sum(score()) /
    5
  • End Sub

Passing array score
Example 4, pp. 338
13
Array Score is passed to a Subprogram
  • Private Sub FillArray(s( ) As Integer)
  • ' Fill array with scores
  • s(1) 85
  • s(2) 92
  • s(3) 75
  • s(4) 68
  • s(5) 84
  • End Sub

This array is pointing to the same location as
array score
14
Array Score is passed to a Function
  • Private Function Sum(s() As Integer) As Integer
  • Dim total As Integer, index As Integer
  • ' Add up scores
  • total 0
  • For index 1 To 5
  • total total s(index)
  • Next index
  • Sum total
  • End Function

15
7.3 Control Array
  • A means of constructing arrays of text boxes,
    labels, and command buttons.
  • At least one element of a control array must be
    created when the form is designed.
  • The remaining elements can be created either
    during form design, or with the Load statement.
    (see next slide)
  • All the properties of the first element are
    passed to other elements of the control array
    including the Top and Left properties.
  • The only property that differs from first element
    is the Visible property.
  • The Load statement sets the Visible property to
    False.

16
Creating Control Array during Form Design
  • Add one instance of the desired control to the
    form.
  • Set the Index property to a number.
  • Set any other properties that will be common to
    all other elements.
  • Click on the control and then press CtrlC.
  • Press Ctrl V, to create the next element.

Page 348.
17
Control Array Event Procedures
  • Even though we may have many elements in the
    txtBox() control array, we will have just one
    txtBox_GotFocus() event procedure to deal with.
  • The value of Index property for the elements of
    the control array must be passed.
  • Example
  • Private Sub txtBox_GotFocus(Index As
    Integer)

See Example 2, pp. 350
18
Creating Control Array at Run Time
  • You can create elements of a control array via
    Load statement at run time.
  • The standard unit of measurement is called Twip
    (1inch 1440 twips).
  • To place a new element of a control array,
    adjust the Top and Left properties during run
    time.
  • Set the Visible property to True.

19
The location and size of a control
20
Example 3, pp. 352 (Creating control array during
run time)
  • Private Sub Form_Load()
  • Dim i As Integer, monthNames As String
  • monthNames "FebMarAprMayJunJulAugSepOctNovDec"
  • For i 1 To 11
  • Load lblMonth(i)
  • Load txtInfo(i)
  • lblMonth(i).Top lblMonth(i - 1).Top
    txtInfo(0).Height
  • txtInfo(i).Top txtInfo(i - 1).Top
    txtInfo(0).Height
  • lblMonth(i).Caption Mid(monthNames, 3 i
    - 2, 3)
  • lblMonth(i).Visible True
  • txtInfo(i).Visible True
  • Next i
  • End Sub

21
7.4 Sorting and Searching
  • A common practice involving arrays is sorting the
    elements of the array in either ascending or
    descending order.
  • You can sort an array in alphabetic order or
    numeric order.
  • There are various methods to sort data items.
  • Types
  • Bubble Sort
  • Shell Sort

22
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 the array is called a
    pass. (See next 2 slides)
  • The maximum number of passes necessary to sort
    the elements in an array is equal to the number
    of elements in the array less 1.
  • The minimum number of passes to sort an array may
    be one.
  • It works well for a small number of data items.
  • It is too slow for really long lists.

23
Bubble Sort
  • For passNum 1 To 4 'Number of passes is 1
    less than

  • number of items
  • For index 1 To 5 - passNum
  • If name(index) gt name(index 1) Then
  • Call SwapData(name( ), index)
  • End If
  • Next index
  • Next passNum

Example 2, pp. 362
24
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

25
Shell Sort
  • Is similar to the bubble sort, but instead of
    comparing and swapping adjacent elements A(count)
    and A(count1), it compares and swaps nonadjacent
    elements A(count) and A(count Gap), where Gap
    starts considerably greater than 1.
  • Gap is set to one-half the length of the array.
  • After each pass if flag is set to 1, then Gap is
    halved again for the next pass.
  • At the end Gap becomes one, and adjacent elements
    are compared and swapped.

26
Shell Sort
  • gap Int(numParts / 2)
  • Do While gap gt 1
  • Do
  • doneFlag 1
  • For index 1 To numParts - gap
  • If part(index) gt part(index gap)
    Then
  • Call Swap( part(index), part(index
    gap)
  • doneFlag 0
  • End If
  • Next index
  • Loop Until doneFlag 1
  • gap Int(gap / 2) 'Halve the length
    of the gap
  • Loop

Example 4, pp. 367
27
Efficiency of Bubble and Shell sortTable 7.6
  • Array Elements Bubble Sort Shell
    Sort
  • 5 10 15
  • 15 105 115
  • 25 300 302
  • 30 435 364
  • 100 4950 2638
  • 500 124,750 22,517

28
Processing Arrays
  • Searching successive elements of an array is
    called Sequential Search.
  • Also called linear search or serial search.
  • A Sequential Search examines each element, from
    the first to the last, until the specified value
    is found or the end of the array is reached.

29
Example of Sequential Search (finding the quiz
grades position greater than 8)
  • Dim quiz(1 To 15) As Single
  • For position 1 TO 15
  • If quiz(position) gt 8 THEN
  • picOutput.Print quiz(position)
  • count count 1
  • End If
  • Next Position

30
Sequential Search
  • Useful for short lists.
  • Very inefficient for long lists (example names in
    telephone book).
  • Use binary search if the list is sorted.
  • In binary search, an ordered array is repeatedly
    divided in half. The half not containing the
    target value is ignored.
  • To use binary search, the data in the array must
    be arranged in ascending or descending order.

31
Binary Search
  • Private Sub BinarySearch(corp As String, result
    As String)
  • foundFlag 0 '1 indicates corp
    found
  • first 1
  • last numFirmsDo While (first lt last) And
    (foundFlag 0) middle Int((first last)
    / 2) Select Case UCase(firm(middle))
    Case corp foundFlag 1 Case
    Is gt corp last middle - 1
    Case Is lt corp first middle 1
    End Select
  • Loop
  • End Sub

If foundFlag 1 Then result "found Else
result "not found End If
32
7.5 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.

33
Declaration of Two-Dimensional Array
  • Syntax
  • Dim arrayname (m1 To n1, m2 To n2) As vartype
  • Example
  • Dim rm (1 To 4, 1 To 4) As Single

column
row
34
Example of a two-dimensional array rm(1 To 4, 1
To 4)
Chicago
Los Angeles
New York
Philadelphia
Chicago
Los Angeles
New York
Philadelphia
rm (2,2)
rm (3,4)
Section 7.5 Example 1
35
Example of Input File
  • 0, 2054, 802, 738
  • 2054, 0, 2786, 2706
  • 802, 2786, 0, 100
  • 738, 2706, 100, 0

36
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

37
Example of two-dimensional array
  • Dim a(1 To 4, 1 To 5) As Integer
  • Private Sub cmdDisplay_Click()
  • For j 1 To 4
  • For k 1 To 5
  • a (j, k) (j - k) j
  • picOutput.Print a(j,k)
  • Next k
  • picOutput.Print
  • Next j
  • End Sub
Write a Comment
User Comments (0)
About PowerShow.com