1. 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.
2. 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)
3. 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.
4. 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.
5. 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 Popeye
An array is a structure containing one or more
subdivisions each of which may contain data
7. 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.00
Thus credit [1] would be the first deposit in the
checking account; credit [2] would be the
second deposit and so on.
10. 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
11. 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.
12. 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].
13. b) Two Dimensional Array
A 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
15. 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
16. 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]
17. 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 salaries
Employee # 1
ID Number: 11111
Salary: 222.22
19. 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.
23. 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.
24. 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
25. 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
26. 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.82
Similarly,
Debit [1] = 14.79, debit [2] = 52.25
Credit array 195.00 491.82 299.95 320.27
Debit array 14.79 52.25 16.13 44.75
27. 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.
28. 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 computation
Arrays help reduce length of algorithm
FUNCTIONS OF ARRAYS
29. 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.
30. 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.
31. 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}
32. 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????
34. 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.
35. 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.
36. DISADVANTAGES OF ARRAYS
It has a single fixed size
Therefore, 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.
37. 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.
38. REFERENCES
Encyclopedia of Computer Science (3rd
Ed) (1993) Edited
by Ralston, A and Reilly E. D. London: Chapman and Hall
French, C. S. (1996) Computer Science. 5th
ed. London:
Book Power
Megson, G.M. (1992) An Introduction to Systolic Algorithm
Design. Oxford: Clarendon
Salmon, 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.com
www.answers.com/topic/arrays