-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_deletation.cpp
More file actions
47 lines (38 loc) · 919 Bytes
/
Copy patharray_deletation.cpp
File metadata and controls
47 lines (38 loc) · 919 Bytes
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
46
47
#include <iostream>
using namespace std;
void deleteElement(int arr[], int& size, int position)
{
// Check if the position is valid
if (position < 0 || position >= size)
{
cout << "Invalid position. Deletion failed." << endl;
return;
}
// Shift elements to the left from the specified position
for (int i = position; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
// Decrease the size of the array
size--;
}
void displayArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
displayArray(arr, size);
int position = 2;
deleteElement(arr, size, position);
cout << "Array after deletion: ";
displayArray(arr, size);
return 0;
}