0% found this document useful (0 votes)
37 views

ICT212: Data Structures and Algorithms: The Array Data Structure

The document discusses arrays as a data structure. It defines an array as an indexed sequence of elements of the same type that are stored in contiguous memory locations. Arrays allow direct access to elements via an index and have fixed sizes. The document covers key aspects of arrays including representation in memory, traversal, insertion and deletion of elements. It also introduces two-dimensional arrays and how they can be represented in memory in row-major or column-major format.

Uploaded by

Leo Logan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

ICT212: Data Structures and Algorithms: The Array Data Structure

The document discusses arrays as a data structure. It defines an array as an indexed sequence of elements of the same type that are stored in contiguous memory locations. Arrays allow direct access to elements via an index and have fixed sizes. The document covers key aspects of arrays including representation in memory, traversal, insertion and deletion of elements. It also introduces two-dimensional arrays and how they can be represented in memory in row-major or column-major format.

Uploaded by

Leo Logan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

ICT212: Data Structures and

Algorithms

The Array Data Structure


The Array
An indexed sequence of components or elements

The indices (or subscripts) are sequential, with upper


and lower bounds

The index provides direct access to the corresponding


component

In an array, data will be stored in a continuous memory


location
What is an Array?
An array is a data structure used for storing a collection of data
items that are all the same type.

By using an array, we can associate a single variable name with


an entire collection of data.

This enables us to save the entire collection of data in adjacent


cells of main memory and to easily reference individual items by
an Index number.

To process an individual item, we need to specify the array


name and indicate which array element is being manipulated.
Storage comparison
Using normal variables
5 8 3 6 2

Var1 Var2 Var3 Var4 Var5

Index

5 (0)
Using an array
8 (1)
3 (2)
6 (3)
2 (4)
MyArray
C++ Arrays

0 1 2 3 4 L-2 L-1

Indices range from 0 to length-1

Each component of the same type

Length fixed when storage is allocated


C-Style Arrays Data Structures:

As an ADT Implemented in C++

ordered indices numbered 0, 1, 2, . . ., CAPACITY - 1


fixed size CAPACITY specifies the capacity of the array
same type elements element_type is the type of elements
direct access subscript operator [] score[0]
score[1]
score[2]
element_type array_name[CAPACITY]; score[3]
.
where .
. .
element_type is any type
array_name is the name of the array — any valid identifier score[99]
CAPACITY (a positive integer constant) is the number of elements
in the array
Subscript operator
[] is an actual operator and not simply a notation/punctuation as in some other
languages.

Its two operands are an array variable and an integer index (or subscript) and is
written
array_name[i]

Here i is an integer expression with 0 < i < CAPACITY – 1.

[] returns the address of the element in location i in array_name;

So array_name[i]is a variable, called an indexed (or subscripted) variable,


whose type is the specified element_type of the array.
Array Example

int MAX_NUM = 100;


Int intStore [MAX_NUM];
int count = 0;
while (count<MAX_NUM)
{
intStore[count] = count*count;
count++;
}
for (int i=count;i>0;i--) {
cout<<intStore[i-1];
}
Size of an Array
The size of an array can be found by the following formula:

N = Ub – Lb + 1

 Where :

Lb is lower index


Ub is the Higher index
N is the array size/length
Array Representation in Memory
The computer memory is made up of cells

Each cell has and is accessed by an address

When an array is declared, the address of the first byte


(or word) in the block of memory associated with the
array is called the base address of the array.

Each array reference must be translated into an offset


from this base address.
Array Representation in Memory
 The address of the th element of array A (A[]) is calculated
by:

A[] = (A) + W*( - Lb)


 Where :
A[] is the address of th element of array A
(A) is the base address
W is the number of words in the cell/Size of data type
Lb is lower index
A is array name
 is index/position number
Array Representation in Memory
char A[8]
0 A 360060 A[6] = ?
1 B
2 C A[6] = 360060 + (1*(6 - 0))
3 D = 360060 + 6
4 E
= 360066
5 F
With char W=1 bytes
6 G ?
7 H
What about A[1] ?
Traversing an Array
The method of visiting each element of an array at least
once is called Traversing.

It is used to perform operations such as:


Reading an element
Printing an element
Processing an element
Traversing an Array - Steps

1. Set index  to Lb (lower bound)

2. Visit the value of 

3. Increase the value of  and compare with Ub (upper

bound)

4. If  < Ub or  = Ub then go to step 2

5. Exit operation
Traversing an Array - Example
Algorithm Averages(A, Ub)
Input array A of U integers
Output averages of A
Average  0
Sum  0
For   Lb to Ub Do

Sum  Sum + A[]

+1

Average  Sum/Ub
Return Average
Exit operation
Insert Component Algorithm
 Insertion of an element into an array can be done at the end or in the middle of
the array.
STEPS:
1. Compare  to Ub

where  is where element is to be inserted

2. If  = (Ub+1) then insert the new element at A[ ]

3. If  <= Ub then:
a. Shift A[Ub], A[Ub-1], A[Ub-2]......,A[] to A[Ub+1], A[Ub], A[Ub-1]......,A[+1]

b. Insert the new element at A[]

4. Increase the values of Ub


Ub=Ub+1.
Insert Component Example
Algorithm InsertEle(A, Ub, pos)
A[8]
Input array A of Ub, pos integers
0 30 360060
Output new A with Ub+1 Elements
1 56
For j  Lb to Ub Do
2 45
If pos  Ub+1
3 21
4 12 A[pos] = 200
5 56 Else
6 890 If pos <= Ub
7 678 For i  Ub to pos Do
A[i+1] = A[i]
200 A[pos] = 200
Ub= Ub+1
Exit operation
Insert Component Example
A[8]
For j  0 to 7 Do A[9]
0 30 360060
If 4  8 //7+1 0 30
1 56
1 56
2 45 A[ 4] = 200
2 45
3 21 Else
3 21
4 12
If 4 <= 7 4 200
5 56
For i  7 to 4 Do 5 12
6 890
A[i+1] = A[i] 6 56
7 678
7 890
A[ 4] = 200
8 678
Ub= 7+1
200
Exit operation
Delete Component Algorithm
STEPS:
 Steps to delete the th element from an array are as follows:

1. Copy the element A[ ] into a variable DeItem

where  is the position of the element to be deleted

2. Shift A[ +1], A[ +2], A[ +3]......,A[Ub] to A[], A[+1], A[ +2]......,A[Ub-1]

4. Decrease the values of Ub

Ub=Ub-1.
Delete Component Example
A[8]
Algorithm DeleteEle(A, Ub, pos)
0 30 360060
1 56 Input array A of Ub, pos integers
2 45 Output new A with Ub-1 Elements
3 21
DeItem  A[pos]
4 12
5 56
For j  pos to Ub-1 Do
6 890
7 678 A[j] = A[j+1]
Delete
Ub= Ub - 1

Exit operation
Delete Component Example
A[8]
A[7]
0 30 360060 DeItem  21
0 30
1 56
1 56
2 45 For j  3 to 6 Do
2 45
3 21
A[j] = A[j+1] 3 12
4 12
4 56
5 56
Ub= 7 - 1 5 890
6 890
6 678
7 678
Exit operation
Delete
Two Dimensional Arrays
This is the method of representing arrays row wise and
column wise

Two dimensional arrays are useful when data needs to


be arranged in a tabular form.

A bidimensional array can be imagined as a


bidimensional table made of elements, all of which are
of the same uniform data type.
Two Dimensional Arrays

0 1 2 3 4
0
1
Stud_Marks 2
3

Stud_Marks[0][0] Stud_Marks[2][4]
2D Arrays - Memory Representation

Two methods are used to represent 2 dimensional


arrays in memory:

Row major Representation

Column major representation


Row major Representation
In this method, Elements are stored row-wise
First row of the array is stored in the first location
Second row is stored in the second location
And so on…
A[1][1]
A[3][2]
A[1][2]
A[1][1] A[1][2] A[2][1]
A[2][1] A[2][2] A[2][2]
A[3][1] A[3][2] A[3][1]
A[3][2]
Column major representation
In this method, Elements are stored Column-wise
First column of the array is stored in the first location
Second column is stored in the second location
And so on…
A[1][1]
A[3][2]
A[2][1]
A[1][1] A[1][2] A[3][1]
A[2][1] A[2][2] A[1][2]
A[3][1] A[3][2] A[2][2]
A[3][2]
Any Questions?

You might also like