Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Similar presentations


Presentation on theme: "CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1."— Presentation transcript:

1 CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1

2 CONTENTS Single Dimension Arrays Declaring Arrays Arrays Manipulation Sorting Arrays: Bubble Sort, Shell Sort Searching Arrays: Linear Search © Prepared By: Razif Razali 2

3 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

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

5 Example One 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)

6 Declaring 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

7 Example: 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 CusN ame (1) CusN ame (2) CusN ame (3) CusN ame (4) CusN ame (5) CusN ame (6) CusN ame (7) CusN ame (8) CusN ame (9) CusN ame (10)

8 Example 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

9 Example Dim studentName(10) As String Dim 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 End End If Next End Sub © Prepared By: Razif Razali 9

10 Output 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

11 Example © Prepared By: Razif Razali 11

12 © Prepared By: Razif Razali 12

13 Multidimensional 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

14 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

15 Two 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

16 SORTING 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

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

18 BUBBLE 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

19 Example of Bubble Sort Have a look at Bubble SortBubble Sort URL : http://www.cs.oswego.edu/~mohammad/classes/csc241/sam ples/sort/Sort2-E.html © Prepared By: Razif Razali 19

20 Shell sort Founded by Donald Shell and named the sorting algorithm after himself in 1959. 1 st 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.

21 Shell sort Shell sort uses a sequence h 1, h 2, …, h t called the increment sequence. Any increment sequence is fine as long as h 1 = 1 and some other choices are better than others.

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

23 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

24 Linear Search Example Linear Search animation Linear Search © Prepared By: Razif Razali 24


Download ppt "CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1."

Similar presentations


Ads by Google