introduction to arrays chapter 7 why use arrays? to store & represent lists of homogeneous...
Post on 20-Dec-2015
216 views
TRANSCRIPT
Introduction to Arrays
Chapter 7
Why use arrays?
• To store & represent lists of homogeneous values
• To simplify program code
• To eliminate the need to reread data
The kinds of problems that benefit from array representation of data• Roll a die (6 sides) 1000 times & record the
frequency of each outcome. Display it.
• Shuffle a deck of 52 cards & then choose 1 card. Record the frequency of each outcome.
• Read in 100 integers (e.g., test scores, prices). Sort them in ascending (or descending) order.
142 175 182162 149 129
Details & terminology• To use an array, we must establish its
subscript range - this must agree with the number of elements the array will have.
• Subscript = position in the array
• Element = the contents of the array at a particular position
• Ex: num(5) is the 5th element in the array called num
Array declaration
• All arrays must be declared using a Dim statement:
Dim num(1 To 25) As Single
Array name
Subscript range
Element type
Where to declare arrays
• Most of the time, arrays are declared as form-level variables
• This means that we Dim them in the “Declarations” section of (General)
– this is done in code view
• Any form-level variable can be used by any procedure or function without being declared as a parameter.
Declaring & referencingDim x (1 To 8) As Single
x: 3 5 12 0 43 -4 82 1
Dim y (1 To 5) As String
y: “abc” “3” “you” “hi” “#”
• x(3), x(7), x(2) + x(5), x(2 + 5)
• y(1), y(4), y(2) & y(3), len(y(5))
• if k = 3, what is x(k)? y(k)? x(k) + 1? x(k+1)? y(k-1) & y(k+1)? y(x(8))?
Common array activities
• Read data values from a user or an input file into an array and process them– some processing requires visiting a data value
more than once, so arrays are recommended or required
• Search an array
• Sort array elements– there are many sorting algorithms to use
Array processing requires iteration (looping)
• If we must process every element of an array, then we use a For…Next loop
• If we will process the array while or until a certain condition is met, then we use a While or Until loop
• Some of this iterative processing requires doubly nested loops– Sorts
Fill an array with user’s dataDim a (1 To 10) As Integer
…...
For j = 1 To 10
a(j) = Val (InputBox(“Enter an integer.”)
Next j
Because we must process every element, we use For...Next
Fill an array with data from an input file
Dim a (1 To 10) As Integer…...
Open “Integers.txt” for input as #1
For j = 1 To 10
Input #1, a(j)
Next j
Close #1Because we must process every
element, we use For...Next
Find the largest element in an array
Dim num (1 To 100) of Any_Type
………...
max = num(1)
For k = 2 to 100
if num(k) > max then
max = num(k)
endif
next kBecause we must process every
element, we use For...Next
Filling arrays at the time that the program is loaded
• Since the array has been declared as a form-level variable, we can use the Form_Load event procedure to fill an array when the program is first loaded to be run:
Private Sub Form_Load()
• See p. 307, Example 2
To recap...
• We have several ways to enter data into an array:– from an input file
– from interactive user input
– from the form_load event procedure
Searching
&
Sorting
Searching
• There is a variety of searching algorithms.
• The fastest ones require that the array elements be sorted (usually in ascending order) first.
• We study the sequential search, which makes no assumptions about the order of elements.
Find the position of the first occurrence of “$$$”
Dim ch (1 To 50) of String…
Let pos = 0Let found = “no”Do While pos < 50 and found = “no”
Let pos = pos + 1if ch(pos) = “$$$” then
Let found = “yes”LoopIf found = “yes” then
Picture1.Print “$$$ was found in position ”; pos
Because we need to process only until we find what we’re looking for, we use a While (or Until) loop
Convert the previous code to a function
• Write a function that receives an array and an object to search for. Return the position of the first occurrence of the item; if the search is unsuccessful, return 0.
• The function header:Private Function find (object As String) As Integer
• The function call:
picBox.Print find (x)(where x = “$$$” from our previous example)
Using & searching parallel arrays
• An Inventory File:Dim product_name (1 To 30) As StringDim product_id (1 To 30) As IntegerDim product_cost (1 To 30) As SingleDim num_in_stock (1 To 30) As IntegerDim num_on_order (1 To 30) As Integer
• We can ask:“How many widgets do we have in stock?”
Sorting• There are many sorting algorithms from
which to choose.
• The most straightforward ones are not usually the fastest, but they get the job done & they’re relatively easy to program.
• Bubble Sort is in this category.– More to come on this
Control arrays (Sec. 7.3)
• We can organize form objects such as text boxes & labels into arrays.– See Example 1, p. 339: Suppose we have five
departments - each one gets its own text box with its own label