Arrays Week3
Arrays Week3
Arrays Week3
DATA STRUCTURES
AND ALGORITHMS
ARRAYS
DEFINITION OF AN ARRAY
An array is a list of contiguous memory cells that store data of the same
type.
An array is a finite, ordered collection of homogeneous data elements.
The idea is to store multiple items of the same type together.
It makes it easier to calculate the position of each element by simply adding
an offset to a base value. i.e the memory location of the first element of the
array (generally denoted by the name of the array)
Array data structure is usually finite and ordered in nature in the sense that it
stores only a known number of data items as per its size in ordered memory
cells.
BASIC TERMINOLOGIES OF ARRAYS
Size: The size of an array refers to the total number of elements that
can be stored in an array.
Name: Every array must have a name. In the case of the figure in the
next slide, the array is called ages.
Type: Type refers to the kind of data stored in an array. The data type
can be an integer, decimal, or textual (string or character).
BASIC TERMINOLOGIES OF ARRAYS
Cell: A single memory location of an array is called a cell. The total number
of cells of an array is equal to the size of the array.
Index: An array index is a positive integer that uniquely identifies a cell of
an array. Indexing of arrays usually starts from zero (0) to the size of the
array minus one (1) in
Pointer: A memory location where data is stored.
CHARACTERISTICS OF ARRAYS
Homogeneity: All elements in an array are of the same data type.
Fixed Size: the size of an array is fixed and must be specified at the time of
declaration. This is static location.
Contiguous Memory Allocation: Elements are stored in contiguous memory
locations which facilitates quick access to elements.
Random Access: Elements can be accessed randomly using indices. The first
element has an index of 0.
STRUCTURE OF AN ARRAY
ARRAY REPRESENTATION
The representation of an array can be defined by its
declaration.
A declaration means allocating memory for an array of a
given size.
2 DIMENSIONAL ARRAYS
Row major storage
Address of A[i][j] = B + W * [ N * (i-Lr) + (j-Lc)]
Column major storage
Address of A[i][j] = B + W * [ (i-Lr) + M * (j-Lc)]
B = Base Address
Lr =Lower bound of row
Lc = Lower bound of column
M = number of rows in the matrix
N = number of columns in the matrix
i = Row subscript of the element whose address is to be found
j = Column subscript of the element whose address is to be found
CALCULATING ADDRESS LOCATION
Row major storage
Address of A[i][j] = B + W * [ N
* (i-Lr) + (j-Lc)]
A[2][1] = 104 + 2 [ 4 * (2 -0) +
(1-0) ]
= 104 + 2 [ 4 * 2 + 1]
= 104 + 2 [8 + 1]
= 104 + 2(9)
= 104 + 18
= 122
CALCULATING ADDRESS LOCATION
Row major storage
Address of A[i][j] = B + W * [ (i-Lr) +
M * (j-Lc)]
A[2][2] = 104 + 2 [ 4 * (2 -0) + (2-0) ]
= 104 + 2 [ 4 * 2 + 2]
= 104 + 2 [8 + 2]
= 104 + 2(10)
= 104 + 20
= 124
APPLICATION OF ARRAYS
Matrix Manipulation: Arrays are efficient in representing and
manipulating matrices, which is essential in various fields like
graphics, scientific computing, and linear algebra. We will cover
operations such as matrix addition, multiplication, and transposition
using arrays.
Strings as an array of characters: strings in many programming
languages are represented as arrays of characters.
String Manipulation: Beyond simple concatenation, arrays are
integral in more complex string manipulations, including substring
extraction, reversal, and searching.
Pattern Matching: arrays play a crucial role in pattern-matching
algorithms, where we search for a specific pattern within a larger set
of data
LIMITATIONS OF ARRAYS