Arrays - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Arrays

Description:

An array is ordered it its values are in either ascending or descending order. ... A. If one name alphabetically precedes the other, copy it onto the third list ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 17
Provided by: fay46
Category:

less

Transcript and Presenter's Notes

Title: Arrays


1
Chapter 7
  • Arrays

2
Ordered Array
  • An array is ordered it 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.

3
Ordered Array
  • An array is ordered it 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.

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

5
Example of Sequential Search (finding the quiz
grades 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

6
Sequential Search
  • Useful for short lists.
  • Very inefficient for long lists ( example names
    in telephone book).
  • Use binary search if the list is sorted.

7
Binary Search
  • 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.

8
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

9
Binary Search
  • If foundFlag 1 Then
  • result "found
  • Else
  • result "not found
  • End If

10
Merging
  • A common practice involving arrays is merging two
    sorted arrays.

11
The method for merging two sorted arrays
  • 1.Compare the two first elements of the first and
    second arrays.
  • A. If one name alphabetically precedes the other,
    copy it onto the third list and cross it off the
    original array.
  • B. If the names are the same, copy the name onto
    the third list and cross out the name from both
    arrays.
  • 2. Repeat Step 1 with the new name in the array
    until you reach the end of either array.
  • 3. Copy the names from the remaining array onto
    the third array.

12
Passing an Array
  • An array can be passed to another procedure by
    reference.

13
Example of Passing an array
  • 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
14
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
15
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

16
Homework
  • P. 341 1-19 Odd Problems
Write a Comment
User Comments (0)
About PowerShow.com