+ All Categories
Home > Documents > Lecture 2a arrays

Lecture 2a arrays

Date post: 22-May-2015
Category:
Upload: victor-palmar
View: 117 times
Download: 1 times
Share this document with a friend
Popular Tags:
38
LECTURE 2: DATA STRUCTURES An array is a data structure consisting of a group of elements that are accessed by indexing. An array is a situation where a set of data items are conveniently arranged into a sequence & referred to by a single identifier. Each data item of an array is known as an element & the elements are referenced by a common name known as the array name An array is a collection of objects of the same data type, allocated contiguously in memory. Individual objects in an array, called elements, are accessed by their position in the array.
Transcript
Page 1: Lecture 2a arrays

LECTURE 2: DATA STRUCTURESAn array is a data structure consisting of a group of

elements that are accessed by indexing.An array is a situation where a set of data items are

conveniently arranged into a sequence & referred to by a single identifier.

Each data item of an array is known as an element & the elements are referenced by a common name known as the array name

An array is a collection of objects of the same data type, allocated contiguously in memory. Individual objects in an array, called elements, are accessed by their position in the array.

Page 2: Lecture 2a arrays

AN ARRAY The position is given by an index, which is also

called a subscript.An array facilitates the coding of repetitive

tasks by allowing the statements executed on each element to be put into a loop that iterates through each element in the array.

An array, also known as a vector or list (for one-dimensional arrays) or a matrix (for two-dimensional arrays)

Page 3: Lecture 2a arrays

ARRAYS

An Array is a variable. It can hold a series of values, essentially, a list of

values.An array provides the logical convenience of being

able to reference all its values by using the single array name.

As a variable, an array is an area in memory, random access memory (RAM) reserved under a name, and referred to by name, into which values can be assigned.

Page 4: Lecture 2a arrays

ARRAYS

An array is a structure that holds multiple values of the same type.

 An array - known as a vector or list (for one-dimensional arrays) or a matrix (for two-dimensional arrays)

It is one of the simplest data structures.

Page 5: Lecture 2a arrays

Examples

E.g. Cartoons = (Popeye, Scooby Doo, Tom & Jerry)

Data Structures = (Stacks, Queues, List)Individual data items are referred to by stating

their position e.g. Cartoons [1] is PopeyeAn array is a structure containing one or more

subdivisions each of which may contain data

Page 6: Lecture 2a arrays

TYPES OF ARRAYS

One Dimensional ArrayTwo Dimensional Array

Page 7: Lecture 2a arrays

a) One Dimensional Array

Within the array, subscripts are used to indicate the individual elements, written as integers within square brackets.

195.00 491.82 299.95 320.00Thus credit [1] would be the first deposit in the

checking account; credit [2] would be the second deposit and so on.

Page 8: Lecture 2a arrays

Credit Array:

[195.00] [491.82] [299.95] [320.00]

Page 9: Lecture 2a arrays

Debit Array:

14.79 52.25 44.7516.13

Page 10: Lecture 2a arrays

One Dimensional Array

The above diagrams shows a credit and Debit Array in which

Credit [1] = 195.00, Credit [2] = 491.82 Credit [3] = 299.95

Similarly, Debit [1] =14.79, Debit [2] =52.25, Debit [3] =16.13, Debit [4] =44.75, Debit [5] = 114.21

Page 11: Lecture 2a arrays

One Dimensional Array

They have a single subscript and are called one-dimensional arrays; often thought of as lists.

In mathematics such arrays are often called vectors.

A one dimensional array is therefore an array having only one subscript & the individual elements are of the same type.

Page 12: Lecture 2a arrays

EXAMPLE 2

To store the salaries of up to 50 employees in a firm we can use an array called salary in which salary [1], salary [2] … salary [50] are real variables that will store the salaries of the first employee, the second employee…up to the 50th employee.

The array contains real elements and the subscripts run over the integer sub range [1………50].

Page 13: Lecture 2a arrays

b) Two Dimensional ArrayA two dimensional array is a generalization of a

linear list, for example an m x n matrix.The rows and column list essentially account

for two dimensional structure of a matrix. An array contains a sequence of elements all of

the same element type. The element type can be any type, even an

array (but not the same array), thus we have an array of arrays

Page 14: Lecture 2a arrays

EXAMPLE

c0 c1 c2 c3

r0 0 1 2 3

r1 1 2 3 4

r2 2 3 4 5

r3 3 4 5 6

r4 4 5 6 7

Page 15: Lecture 2a arrays

EXAMPLE

Here, we have an array with five rows and four columns.

It has twenty total elements. However we say it has dimension five by

four, not dimension twenty

Page 16: Lecture 2a arrays

EXAMPLE

We have a pay array containing 50 rows with 26 real elements on each row. Each row represents an employee and each column a pay period i.e. ( a two – dimensional array ).

Pay [1,1] Pay [1,2] Pay [1,3] ……Pay [1, 26]Pay [2,1] Pay [2,2] Pay [2,3] ……Pay [2, 26]Pay [3,1] Pay [3,2] Pay [3,3] ……Pay [3, 26]: : : : :Pay [50,1]Pay [50,2] Pay[50,3]………………………

Pay [50,26]

Page 17: Lecture 2a arrays

c) Parallel Arrays As an application of one dimensional array, we

begin the construction of a payroll program to read and display employees’ salaries for a small business e.g.

Enter employees’ ID numbers and salariesEmployee # 1ID Number: 11111Salary: 222.22

Page 18: Lecture 2a arrays

Contd...

Employee # 2ID Number: 11112Salary: 333.33 Employee # 3ID Number: 11113Salary: 444.44Etc.

Page 19: Lecture 2a arrays

Example

The program will need to store a two – part record for each employee, containing both the integer employee number & ID– number salary.

An employee record is therefore heterogeneous & cannot be stored entirely in one array.

Instead we will use a pair of arrays, ID & salary which are said to be parallel arrays, because their subscripts track together: i.e. salary [I] & ID [I] together make up the data record for the 1st employee.

Page 20: Lecture 2a arrays

Example

Salary [1] Salary [2] Salary [3]

Page 21: Lecture 2a arrays

Example

ID [1] Salary [2] Salary [3]

Page 22: Lecture 2a arrays

Question

Class, What are the Components of an

Array???

Page 23: Lecture 2a arrays

COMPONENTS OF AN ARRAY

An array consists of elements, data type, subscript & the square brackets.

Arrays hold a series of data elements, usually of the same size & data type.

The series could be of data types such as integers, strings or even floats.

Page 24: Lecture 2a arrays

COMPONENTS OF AN ARRAY

Individual elements are accessed by their position in the array.

The position is given by an index, which is also called a subscript.

The index usually uses a consecutive range of integers

Page 25: Lecture 2a arrays

CHARACTERISTICS OF ARRAYS

Homogeneous: They store one type of data elements.

Cartesian: Arrays allow their access through an index. This referencing is possible because arrays have subscripts and elements that enable access via index

Page 26: Lecture 2a arrays

CHARACTERISTICS OF ARRAYS

Subscripts: In Pascal the subscripts are written as integers within square brackets thus credit [1] would be the first deposit in the checking account, credit [2] would be the second deposit and so on.

Example[1] = 195.00 and credit [2] = 491.82Similarly,Debit [1] = 14.79, debit [2] = 52.25Credit array 195.00 491.82 299.95 320.27Debit array 14.79 52.25 16.13 44.75

Page 27: Lecture 2a arrays

CHARACTERISTICS OF ARRAYS

We use subscripts to access the individual elements i.e. in a one-dimensional array, it has only one subscript & is imagined as a list or vector.

The elements are stored in sequence in memory beginning with the address of the lowest-subscripted element.

 A two dimensional array has two subscripts & is imagined as a table with rows & columns.

Page 28: Lecture 2a arrays

Form the basis for several more complex data structures, e.g. hash tables, stacks and queues.

Arrays provide a very special way of sorting or organising data in a computers memory

Arrays serves as parameters in computationArrays help reduce length of algorithm

FUNCTIONS OF ARRAYS

Page 29: Lecture 2a arrays

OPERATIONS ON ARRAYS

Creating an array: The create function produces a new, empty array of the appropriate size.

Assigning the array: Once an array has been created a value from a field or variable, is then assigned to an element of the array. The syntax used is:

Array name {element number}:= value. Array elements are numbered by their position in the array, starting at one and going up.

Additionally, every array has zero element number.

Page 30: Lecture 2a arrays

OPERATIONS ON ARRAYS

Retrieving : Retrieve operation accepts an array and an index.

This returns the value associated if the index is valid, or error if the index is invalid.

Searching : Searching methods are designed to take advantage of the organization of information & thereby reduce the amount of effort to either locate a particular item or to establish that it is not present in a data set.

Page 31: Lecture 2a arrays

OPERATIONS ON ARRAYS

Sorting and Merging: Sorting & merging provide us with a means of organizing information to facilitate the retrieval of specific data.

Field or variable: = array name {element number}

Page 32: Lecture 2a arrays

OPERATIONS ON ARRAYS

Displaying arrays: To display the contents of array on a form, you simply give an active object capable of displaying an array

The contents of the array will then automatically be displayed in that object. The objects that can display the contents of an array are: Pop-up Lists/Drop-down Lists, and Combo Boxes.

AN EXAMPLE????

Page 33: Lecture 2a arrays

APPLICATION OF ARRAYS

CLASS DISCUSSION

Page 34: Lecture 2a arrays

ADVANTAGES OF ARRAYS

Arrays permit efficient random access in constant time making them most appropriate for storing a fixed amount of data which will be accessed in an unpredictable fashion.

This means that the time taken to access a[i] is the same for each index.

Iterating through an array has good locality of reference, and so is much faster than iterating through a linked list of the same size, which tends to jump around in memory.

Page 35: Lecture 2a arrays

ADVANTAGES OF ARRAYS

Compact data structures; storing 100 integers in an array takes only 100 times the space required to store an integer, takes no additional storage space.

Arrays simplify algorithms by compressing repetitive iterations, as they perform computations on collections of data with common attribute.

Page 36: Lecture 2a arrays

DISADVANTAGES OF ARRAYS

It has a single fixed sizeTherefore, there are some indexes which refer

to invalid elements, e.g., the index 17 in an array of size 5.

This brings range error, since it is difficult to allow a subscript to reach a value outside its declared range, as this will make a compiler to generate an error.

Page 37: Lecture 2a arrays

DISADVANTAGES OF ARRAYS

Multidimensional arrays cannot be accessed by simple indexing & these incur additional overheads in programming its access.

For a large number of programming languages, except for Java, the array sizes must be known at compile time, since once space is allocated it cannot be extended, as there is no guarantee that the next block of memory will be free at some time later in the execution making arrays static data structures.

Page 38: Lecture 2a arrays

REFERENCESEncyclopedia of Computer Science (3rd Ed) (1993) Edited

by Ralston, A and Reilly E. D. London: Chapman and HallFrench, C. S. (1996) Computer Science. 5th ed. London:

Book PowerMegson, G.M. (1992) An Introduction to Systolic Algorithm

Design. Oxford: ClarendonSalmon, W. I. (1991) Structures and Abstractions: An

introduction to Computer Science with Pascal. Boston. Irwin Inc.

Society Schools Committee (1998) A glossary of Computing terms (9th ed.) England: Longman

Williams, P.M. (2004) Data Structures. Sussex: University of Sussex Computer Science and Artificial Intelligence.

http:/developer.sun.comwww.answers.com/topic/arrays


Recommended