-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionAndDeletion.cpp
45 lines (45 loc) · 1.07 KB
/
InsertionAndDeletion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
using namespace std;
void display(int* arr, int size)
{
for(int i=0;i<size;i++){
cout << arr[i] <<" ";
}
cout << endl;
}
void insert(int* arr, int size, int element, int pos)
{
for(int i=size-1;i>=pos;i--){
arr[i+1] = arr[i];
}
arr[pos] = element;
size += 1;
cout << "The array after insertion is: ";
display(arr, size);
}
void del(int* arr, int size, int pos){
for(int i=pos;i<=size-1;i++){
arr[i] = arr[i+1];
}
size -= 1;
cout << "The array after deletion is: ";
display(arr, size);
}
int main()
{
int size, element, index;
cout << "Enter the size of the array: ";
cin >> size;
int *arr = new int [size];
cout << "Enter the elements of the array: ";
for(int i=0;i<size;i++){
cin >> arr[i];
}
cout << "Enter the element to be inserted and it's position: ";
cin >> element >> index;
insert(arr, size, element, index);
cout << "Enter the position whose element is to be deleted: ";
cin >> index;
del(arr, size, index);
return 0;
}