CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

24
CHAPTER EIGHT CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali 1

description

INTRODUCTION By definition, an array is a list of variables, all with the same data type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item. For example, if we need to enter one hundred names, we might have difficulty in declaring 100 different names, this is a waste of time and efforts. So, instead of declaring one hundred different variables, we need to declare only one array. We differentiate each item in the array by using subscript, the index value of each item, for example name(1), name(2),name(3) etc., which will make declaring variables streamline and much systematic. © Prepared By: Razif Razali 3

Transcript of CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Page 1: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

CHAPTER EIGHTCHAPTER EIGHTARRAYS

© Prepared By:Razif Razali 1

Page 2: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

CONTENTSCONTENTS

Single Dimension ArraysDeclaring ArraysArrays ManipulationSorting Arrays: Bubble Sort, Shell

SortSearching Arrays: Linear Search

© Prepared By:Razif Razali

2

Page 3: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

INTRODUCTIONINTRODUCTION By definition, an array is a list of variables, all with the same data

type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal

with, we need to declare an array of variables instead of using a variable for each item.

For example, if we need to enter one hundred names, we might have difficulty in declaring 100 different names, this is a waste of time and efforts.

So,  instead of declaring one hundred different variables, we need to declare only one array. 

We differentiate each item in the array by using subscript, the index value of each item, for example name(1), name(2),name(3) .......etc. , which will make declaring variables streamline and much systematic.

© Prepared By:Razif Razali

3

Page 4: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

DIMENSION OF ARRAYDIMENSION OF ARRAY An array can be one dimensional or multidimensional. One dimensional array is like a list of items or a table

that consists of one row of items or one column of items. A two dimensional array will be a table of items that

make up of rows and columns. While the format for a one dimensional array is

ArrayName(x), the format for a two dimensional array is ArrayName(x,y) while a three dimensional array is ArrayName(x,y,z) .

Normally it is sufficient to use one dimensional and two dimensional array ,you only need to use higher dimensional arrays if you need with engineering problems or even some accounting problems.

© Prepared By:Razif Razali

4

Page 5: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

ExampleExampleOne dimensional Array

Two Dimensional Array

© Prepared By:Razif Razali

5

Student Name

Name (1)

Name (2)

Name (3)

Name (4)

Name (5)

Name (1,1) Name (1,2) Name (1,3) Name (1,4)Name (2,1) Name (2,2) Name (2,3) Name (2,4)Name (3,1) Name (3,2) Name (3,3) Name (3,4)

Page 6: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Declaring ArraysDeclaring Arrays

We could use Public or Dim statement to declare an array just as the way we declare a single variable.

The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure.

The general format to declare a one dimensional array is as follow:

Dim arrayName(subs) as dataType where subs indicates the last subscript in the array.

© Prepared By:Razif Razali

6

Page 7: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Example: Declaring an arrayExample: Declaring an array

Dim CusName(10) as String Will declare an array that consists of 10 elements if the

statement Option Base 1 appear in the declaration area, starting from CusName(1) to CusName(10).

Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10)

© Prepared By:Razif Razali

7

CusName (1)

CusName (2)

CusName (3)

CusName (4)

CusName (5)

CusName (6)

CusName (7)

CusName (8)

CusName (9)

CusName (10)

Page 8: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

ExampleExample

Dim Count(100 to 500) as Integer Declares an array that consists of the first

element starting from Count(100) and ends at Count(500)

Eg. 4 student results stored in an array Dim result(1 TO 4)as Single

Array result has 4 elements, each of single type

© Prepared By:Razif Razali

8

Page 9: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

ExampleExampleDim studentName(10) As StringDim num As Integer

Private Sub cmdStart_Click()For num = 1 To 10 studentName(num) = InputBox("Enter the student name", "Enter

Name", "", 1500, 4500)If studentName(num) <> "" Then Form1.Print studentName(num)Else EndEnd If NextEnd Sub

© Prepared By:Razif Razali

9

Page 10: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

OutputOutput The above program accepts data entry through an input

box and displays the entries in the form itself. As you can see, this program will only allows a user to enter 10 names each time he click on the start button.

© Prepared By:Razif Razali

10

Page 11: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

ExampleExample

© Prepared By:Razif Razali

11

Page 12: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

© Prepared By:Razif Razali

12

Page 13: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Multidimensional ArraysMultidimensional Arrays

Arrays can have multiple dimensions. A common use of multidimensional arrays is to represent tables

of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two

indexes. The first (by convention) identifies the element's row and the

second (by convention) identifies the element's column. Tables or arrays that require two indexes to identify a particular

element are called two dimensional arrays. Note that multidimensional arrays can have more than two

dimensions. Visual Basic supports at least 60 array dimensions, but most

people will need to use more than two or three dimensional-arrays.

© Prepared By:Razif Razali

13

Page 14: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Multidimensional Arrays

The following statement declares a two-dimensional array 50 by 50 array within a procedure.

Dim AvgMarks ( 50, 50) As Integer It is also possible to define the lower limits for one or both

the dimensions as for fixed size arrays. An example for this is given here. Dim Marks ( 101 To 200, 1 To 100) As Integer

An example for three dimensional-array with defined lower limits is given below.Dim Details( 101 To 200, 1 To 100, 1 To 100) As

Integer

© Prepared By:Razif Razali

14

Page 15: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Two Dimensional ArrayTwo Dimensional Array

Some arrays have two dimensions, such as the number of offices on each floor of each building on a campus.

The specification of an element requires both the building number and the floor, and each element holds the count for that combination of building and floor.

Therefore, such an array uses two indexes. The following example declares a variable to hold a

two-dimensional array of office counts, for buildings 0 through 40 and floors 0 through 5.

Dim officeCounts(40, 5) As Byte A two-dimensional array is also called a rectangular

array.

© Prepared By:Razif Razali

15

Page 16: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

SORTINGSORTING

Sorting is any process of arranging items in some sequence and/or in different sets, and accordingly, it has two common, yet distinct meanings:◦ Ordering: arranging items of the same kind, class,

nature, etc. in some ordered sequence,◦ Categorizing: grouping and labeling items with

similar properties together (by sorts).Sorting is an operation that you often perform on

arrays. As you probably know, there are dozens of different

sort algorithms, each one with its strengths and weaknesses.

© Prepared By:Razif Razali

16

Page 17: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Why we do sorting?

Commonly encountered programming task in computing.

Examples of sorting:◦ List containing exam scores sorted from

Lowest to Highest or from Highest to Lowest◦ List containing words that were misspelled

and be listed in alphabetical order.◦ List of student records and sorted by student

number or alphabetically by first or last name.

Page 18: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

BUBBLE SORTBUBBLE SORT

Bubble sort is a straightforward and simplistic method of sorting data that is used in computer science education.

The algorithm starts at the beginning of the data set. It compares the first two elements, and if the first is

greater than the second, then it swaps them. It continues doing this for each pair of adjacent elements

to the end of the data set. It then starts again with the first two elements, repeating

until no swaps have occurred on the last pass. This algorithm is highly inefficient, and is rarely used,

except as a simplistic example.

© Prepared By:Razif Razali

18

Page 19: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Example of Bubble SortExample of Bubble Sort

Have a look at Bubble SortURL :

http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html

© Prepared By:Razif Razali

19

Page 20: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Shell sort

Founded by Donald Shell and named the sorting algorithm after himself in 1959.

1st algorithm to break the quadratic time barrier but few years later, a sub quadratic time bound was proven

Shell sort works by comparing elements that are distant rather than adjacent elements in an array or list where adjacent elements are compared.

Page 21: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Shell sort

Shell sort uses a sequence h1, h2, …, ht called the increment sequence.

Any increment sequence is fine as long as h1 = 1 and some other choices are better than others.

Page 22: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Shell sort

Shell sort makes multiple passes through a list and sorts a number of equally sized sets using the insertion sort.

Shell sort improves on the efficiency of insertion sort by quickly shifting values to their destination.

Page 23: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Searching – Linear Search

In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists in checking every one of its elements, one at a time and in sequence, until the desired one is found.

Linear search is the simplest search algorithm; it is a special case of brute-force search.

Its worst case cost is proportional to the number of elements in the list; and so is its expected cost, if all list elements are equally likely to be searched for.

Therefore, if the list has more than a few elements, other methods (such as binary search or hashing) may be much more efficient.

© Prepared By:Razif Razali

23

Page 24: CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Linear Search ExampleLinear Search ExampleLinear Search animation

© Prepared By:Razif Razali

24