Lesson 04
Lesson 04
int a[3];
1
Arrays
❑ Element − Each item stored in an array is called an element.
❑ Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
int a[3];
2
Array Representation
❑ Arrays can be declared in various ways in different languages. For illustration, let's
take C++ array declaration.
3
Array Representation
❑ As per the below illustration, following are the important points to be considered.
❑ Index starts with 0.
❑ Array length is 10 which means it can store 10 elements.
❑ Each element can be accessed via its index. For example, we can fetch an
element at index 6 as 19.
4
Basic Operations on Arrays
Following are the basic operations supported by an array.
❑ Traverse − print all the array elements one by one.
❑ Insertion − Adds an element at the given index.
❑ Deletion − Deletes an element at the given index.
❑ Search − Searches an element using the given index or by the value.
❑ Update − Updates an element at the given index.
6
Insertion
❑ Insertion refer to the process of adding a new element to an existing one
dimensional array.
❑ Insertion can be at
❑ Any random position within array
❑ The end of an array
❑ Inserting an element at any arbitrary position, all the elements starting from the
specified position are shifted towards right to make a vacant position for new
insertion.
❑ Insertion at the end is very easy process as there is no shifting of elements.
7
Method for Insertion Operation
❑ Insertion at any arbitrary position
❑ Lets take array of size 6 elements.
00 11 22 33 44 65
10 20 30 40
8
Cont...
0 1 2 3 4 5
10 20 30 40
0 1 2 3 4 5
10 20 30 40
0 1 2 3 4
25 35 40
Inserted here
10
Algorithm for Insertion Operation
Step 1: Start
Step 2: i n-1
Step 3: Repeat steps 4 and 5 while i >= k
Step 4: a[i+1] a[i]
Step 5: ii -1
[end of while loop]
Step 6: a[k] item
Step 7: n n+1
Step 8: end
Note: n denotes the size of an array, k denotes the position of new element for insertion.
11
Deletion
❑ Deletion refers to a process of removing an existing element from an array.
❑ To delete an element, first search it, using a searching technique.
❑ After locating the specified element, remove it from array.
❑ When the specified element is removed then shift upwards all the elements to
maintain the order of an array.
❑ If the specified element for deletion is the last element, then it is very easy to
delete to as there is no need of shifting of remaining element.
12
Method for Deletion Operation
❑ Lets take array of size element.
0 1 2 3 4 5
10 20 30 40 50 60
13
Method for Deletion Operation
❑ When Search method locate the element 40 at index 3.
❑ Now delete the element 40 from array in such a way that the element at index 4
shifted to index 4 shifted to index 3 and element at index 5 shifted to index 4 so on.
0 1 2 3 4 5
10 20 30 50 60
14
Cont...
❑ Now to maintain the order of elements in an array, element 60 shift to index 4.
0 1 2 3 4 5
10 20 30 50 60
15
Algorithm for Deletion Operation
Step 1: start
Step 2: Repeat for i=k to n-1
Step 3: a[i] a[i + 1 ]
[end of for loop]
Step 4: n n-1
Step 5: end
16
Advantages of Array
❑Convenient syntax
20
17
Disadvantage of Array
21
18
Assignment
• Create a Program to Implement Insertion and Deletion operation on
array.
• Representation of Array
• Implementation in C++
Any Questions?
Next Topic
Sorting
25