-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_imertion-deletion-1d-array.cpp
More file actions
45 lines (36 loc) · 1 KB
/
Copy path05_imertion-deletion-1d-array.cpp
File metadata and controls
45 lines (36 loc) · 1 KB
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 insertElement(int arr[], int &size, int element, int position)
{
for (int i = size; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element; // Insert element
size++;
}
void deleteElement(int arr[], int &size, int position) {
// Move elements left
for (int i = position; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--; // Reduce size
}
void displayArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[10] = {1, 2, 3, 4, 5}; // Array with initial values
int size = 5; // Current size
cout << "Before operations: ";
displayArray(arr, size);
insertElement(arr, size, 99, 2); // Insert 99 at index 2
cout << "After insertion: ";
displayArray(arr, size);
deleteElement(arr, size, 3); // Delete element at index 3
cout << "After deletion: ";
displayArray(arr, size);
return 0;
}