Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1

2 Introduction to Arrays Chapter 7

3 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread data

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

5 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

6 Array declaration All arrays must be declared using a Dim statement: Dim num (1 To 25) As Single Array name Subscript range Element type

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

8 Declaring & referencing Dim 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))?

9 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

10 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

11 Fill an array with user’s data Dim 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

12 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 #1 Because we must process every element, we use For...Next

13 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 k Because we must process every element, we use For...Next

14 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

15 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

16 Searching & Sorting

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

18 Find the position of the first occurrence of “$$$” Dim ch (1 To 50) of String … Let pos = 0 Let found = “no” Do While pos < 50 and found = “no” Let pos = pos + 1 if ch(pos) = “$$$” then Let found = “yes” Loop If 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

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

20 Using & searching parallel arrays An Inventory File: Dim product_name (1 To 30) As String Dim product_id (1 To 30) As Integer Dim product_cost (1 To 30) As Single Dim num_in_stock (1 To 30) As Integer Dim num_on_order (1 To 30) As Integer We can ask: “How many widgets do we have in stock?”

21 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

22 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


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

Similar presentations


Ads by Google