ICT212: Data Structures and Algorithms: The Array Data Structure
ICT212: Data Structures and Algorithms: The Array Data Structure
Algorithms
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
Its two operands are an array variable and an integer index (or subscript) and is
written
array_name[i]
N = Ub – Lb + 1
Where :
bound)
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
+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
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]
2. Shift A[ +1], A[ +2], A[ +3]......,A[Ub] to A[], A[+1], A[ +2]......,A[Ub-1]
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
0 1 2 3 4
0
1
Stud_Marks 2
3
Stud_Marks[0][0] Stud_Marks[2][4]
2D Arrays - Memory Representation