Lab Report 05
Lab Report 05
Analysis:
In the insertion operation, we are adding one or more elements to the array. Based on the requirement,
a new element can be added at the beginning, end or any given index of array. Following can be a
situation with array insertion:
• Insertion at the beginning of an array
• Insertion at the given index of an array
• Insertion after the given index of an array
• Insertion before the given index of an array
• Insertion at the end of an array
Algorithm:
Insertion at the beginning of an array
IF N = MAX, return
ELSE
N=N+1
A[FIRST] = New_Element
A[index] = New_Element
A[index + 1] = New_Element
A[index - 1] = New_Element
Source Code:
#include <iostream>
using namespace std;
int main() {
int arr[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;
return 0;
}
Input-Output
01
Analysis:
In the deletion operation, we are deleting one or more elements from the array. Based on the
requirement, an element can be deleted from the beginning, end or any given index of array.
Following can be a situation with array deletion:
• Delete from the beginning of an array
• Delete from the given index of an array
• Delete after the given index of an array
• Delete before the given index of an array
• Delete from the end of an array
Algorithm:
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Source Code:
#include <iostream>
using namespace std;
int main() {
int arr[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;
deleteFromBeginning(arr, size);
cout << "From the beginning\t: ";
displayArray(arr, size);
cout<< endl;
deleteFromEnd(arr, size);
cout << "From the end\t\t: ";
displayArray(arr, size);
return 0;
}
Input-Output
01