Top Banner
1 Chapter 7 Arrays
56

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.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

1

Chapter 7

Arrays

Page 2: 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.

2

Outline and Objective

In this chapter we will•Learn about arrays

•One-dimensional arrays

•Two-dimensional arrays

•Learn about searching

•Learn about sorting

Page 3: 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.

Array verses Simple Variable

Simple variable is used to store a single value.Ex: Dim X as Integer 0

X

0000X(1) X(2) X(3) X(4)

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

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

4

Elements of an Array

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)

0000X(1) X(2) X(3) X(4)

X

Page 5: 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.

5

Array Grade()

Dim Grade( 1 To 4) As Integer

35 10 5 25

Grade(1)

Array Name

Index

Page 6: 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.

6

Array Declaration

SyntaxDim arrayName(m To n) As varTypewhere m and n are integers

Declaring arrays - specify:•name

•type of array

•number of elements

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

7

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)

00 0

stdID(1) stdID(2) stdID(3)This statement

creates array of 3 integer elements

Page 8: 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.

8

The Dim Statement

Used to declare an array A Dim statement must occur before

the first reference to the array elements.

Page 9: 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.

9

Initializing an Array

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

0

0

0

0

0

Grade(1)

Grade(2)

Grade(3)

Grade(4)

Grade(5)

5

30

25

15

30

15

25

Page 10: 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.

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

000 0

x(0) x(1) x(2) x(3)

000 0

x(0) x(1) x(2) x(3)

Page 11: 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.

Example

Dim Y(-1 to 2) As Integer

000 0

Y(-1) Y(0) Y(1) Y(2)

Dim Z(-2 to 0) As Single

00 0

Z(-2) Z(-1) Z(0)

Page 12: 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.

Example

Dim Y(-1 to -1) As Integer

0

Y(-1)

Dim Z(2 to 2) As Single

0

Z(2)

Page 13: 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.

Example

Dim Y(2) As Integer

Dim Z(2 to 2) As Single

0

Z(2)

000

Y(0) Y(1) Y(2)

Page 14: 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.

Example Dim Y(10 to 1) As Integer

• Compile ERROR

Dim Z(2 to -2) As Single

• Compile ERROR

Page 15: 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.

Example

i = 5Dim arr(i) As Integer

• Invalid statements

Page 16: 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.

Example

5

0

0

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

Example

Page 18: 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.

Example

m, n must be a constant

Page 19: 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.

Chapter 7 - Visual Basic Schneider

19

Initializing an Array by Reading from a File

Dim student (1 To 30) As StringDim count As IntegerOpen “STUDENTS.txt” For Input As #1For count = 1 To 30 Input #1, student(count)Next count

Ali

Ahmed

..

..

..

Huda

STUDENTS.txt

Ali Ahmed HudaStudent

1 2 30

Page 20: 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.

Chapter 7 - Visual Basic Schneider 20

Adding Up Elements in an ArrayDim score(1 To 30) As Single, student(1 To 30) As StringDim count As Integer, average as Integer, sum As IntegerOpen “STUDENT.TXT” For Input As #1For count = 1 To 30 Input #1, student(count), score(count)Next countsum = 0For count = 1 To 30 sum = sum + score(count)Next countaverage = sum/30

Ali Ahmed HudaStudent

1 2 3022 19 25Score

1 2 30

Ali 22

Ahmed 19

..

..

..

Huda 25

Students.txt

Page 21: 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.

21

Dim XY (1 To 5) As IntegerDim count As Integercount = 1Open “DATA.TXT” For Input As #1Do While NOT EOF(1) Input #1, XY(count)

count= count + 1Loop

Example

Page 22: 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.

22

Parallel Arrays

Two arrays are said to be parallel if subscripted variables having the same subscript are related.

Ali Ahmed HudaStudent

1 2 3022 19 25Score

1 2 30

Ali 22

Ahmed 19

..

..

..

Huda 25

Students.txt

Page 23: 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.

23

Example of Parallel Arrays

Dim nom(1 To 3) As String, score(1 To 3) As Integer

Dim student As IntegerOpen “SCORE.TXT” For Input As #1For student = 1 To 3 Input #1, nom(student), score(student)Next studentClose #1

Page 24: 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.

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

Page 25: 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.

Dynamic Arrays

Defines or dimensions a Dynamic array•ReDim arrayName (m To n) as varType

•m, n can be a variables or expressions

ReDim can only be used inside a procedure

Use Dim in General Declaration as•Dim arrayName() as varType

•Cannot be used till you ReDim it in a procedure

25

Page 26: 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.

About ReDim:

Dim A(5) as integer ReDim A(7) Invalid

Redim A(5) Dim A(7) Invalid

ReDim A(5) ReDim A(7) as integer Invalid

Page 27: 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.

About ReDim:

Dim A() as integer Redim A(7) as integer Valid

X=1 ReDim A(x) as integer Valid

Dim A() as integer A(1)= 5 Invalid

Page 28: 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.

28

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.

403010 15

x(0) x(1) x(2) x(3)

Page 29: 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.

Chapter 7 - Visual Basic Schneider 29

Merging two ordered 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.

Page 30: 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.

Basem WalidStudent1

1 2

Ahmed MajidStudent2

1 2

Student3

1 2

Osama

3 4

Nabeel

Ali

3 4

Salim

3 4 5 6 7 8

Merging two ordered arrays

SalimAhmed Ali Basem Majid Nabeel Osama Walid

Page 31: 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.

15

0

Arr1(1)

Arr1(2)

Arr1(3)

5

13

26

0

0

Arr2(1)

Arr2(2)

Arr2(3)

10

20

0

0

0

0

0

0Is 5 <= 10 T: Copy the number 5 from Arr1 to Arr3, cross the number 5 off the original array (Arr1)

Arr3

5 10 5

13

10

101313 20

13

20

15

20

15

20

20

Is 13 <=10 F: Copy the number 10 from Arr2 to the Arr3, cross it off the original array

Repeat the step with the new number in the array until you reach the end of either array.Copy the numbers from the remaining array onto the third array.

Merging two ordered arrays

026

Page 32: 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.

32

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

Page 33: 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.

33

Example

Page 34: 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.

Example

Page 35: 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.

Example

Page 36: 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.

36

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 highestdescending: highest to lowest

Page 37: 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.

37

Bubble Sort

The bubble sort involves comparing adjacent elements and swapping the values of those elements when they are out of order.

Page 38: 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.

38

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

Page 39: 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.

39

Efficiency of Bubble and Shell sort (average number of comparisons)

Array Elements Bubble Sort Shell Sort

5 10 1515 105 11525 300 30230 435 364100 4,950 2,638500 124,750 22,517

Page 40: 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.

40

Searching Arrays The process of finding the position of a value

in an array is called searching For example

Search(-10) = 3Search (3) = 1Search (5) = Not Found

-103 6

stdID(1) stdID(2) stdID(3)

Page 41: 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.

41

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(4) = 5

-103 7

stdID(1) stdID(2) stdID(3)

101 4

stdID(4) stdID(5) stdID(6)

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

Page 42: 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.

42

The Sequential Search

Dim nom(1 To 6) As IntegerDim index As Integer, foundFlag As BooleanDo While (index <= 6) And (Not foundFlag) If nom(index)= 4 Then

foundFlag = True Else index = index + 1 End IfLooppicOutput.Print “The Number 4 is located at position“;

index

Page 43: 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.

43

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.

Page 44: 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.

44

Binary Search

In a binary search, an ordered array is repeatedly divided in half. The half not containing the target value is ignored.

Page 45: 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.

8

3

7

stdID(1)

stdID(2)

stdID(3)

77

10

22

stdID(4)

stdID(5)

stdID(6)

81

78

80

100

91

92

stdID(7)

stdID(8)

stdID(9)

stdID(10)

stdID(11)

stdID(12)

Search(78) =

First = 1

Last = 12

Middle = Int((first + last) / 2)

Middle = Int(1+12)/2=6

Middle=6

Is 78 = 77 falseIs 78< 77 falseIs 78>77First =7

First = 7

Middle = Int(7+12)/2=9

Middle= 9

Is 78 = 81 falseIs 78 < 81 Last = 8

Last = 8

Middle = Int(7+8)/2=7

Middle = 7

Is 78 = 78 trueSTOPSearch(78) =7

Page 46: 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.

46

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.

Page 47: 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.

47

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

Page 48: 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.

48

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

Page 49: 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.

Examples: How many elements?

Dim rm(0 To 3, 97 To 99) As Integer

49

00 0

00 0

00 0

00 0

9997 98

0

1

2

3

12 elements

Page 50: 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.

What is the output of:

Dim (1 to 3 , 1 to 3) as integerFor i = 1 To 3 For j = 1 To 3 a(i, j) = i * j Print a(i, j); Next j PrintNext i

The output is

1 2 32 4 63 6 9

Page 51: 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.

Examples: How many elements?

Dim rm(1 To 4) As Single

51

00 0

rm(1) rm(2) rm(3)

0

rm(4)

4 elements

Page 52: 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.

Examples: How many elements?

Dim arr(4, 1 To 4) As Single

52

00 0

00 0

00 0

00 0

31 2

0

1

2

3

0

0

0

0

4

00 0 04

20 elements

Page 53: 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.

Examples: How many elements?

Dim arr(-5 To -3, 5 To 5) As String

53

5

-5

-4

-3

3 elements

Page 54: 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.

Examples: How many elements?

Dim xy(-3 To -5) As Integer

54

ERROR

Page 55: 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.

Examples

Dim arr(0 To 3, 97 To 99) As Integer•arr(1,98)= 20

•arr(3,99)=15

•arr(0,2) Error: index out of range

•arr(-1,99) Error: index out of range

55

00 0

00 20

00 0

150 0

9997 98

0

1

2

3

Page 56: 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.

Examples

Dim arr(0 To 3, 97 To 99) As IntegerFor i = 97 To 99

arr(1,i) = 10

Next i

56

00 0

1010 10

00 0

00 0

9997 98

0

1

2

3