-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp-data-structures-array-delete.cpp
50 lines (38 loc) · 1.17 KB
/
cpp-data-structures-array-delete.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
46
47
48
49
50
#include <iostream>
#include<stdlib.h>
using namespace std;
// (->) arrow operator is to access members of a structure through a pointer
struct Array
{
int A[10];
int size;
int length;
};
void Display(struct Array arr) // function to display the array's elements
{
int i;
cout << "Elements are " << endl;
for (i = 0; i < arr.length; i++)
cout << " " << arr.A[i];
}
int Delete(struct Array *arr, int index) // defining an array pointer and given index parameter
{
int x = 0;
int i; // created to shift values
if (index >= 0 && index < arr->length) // checking the index length
{
x = arr->A[index]; // x becomes the value from a given index
for (i = index; i < arr->length - 1; i++) // starting at index, i should be less than the length -1 because of the shift
arr->A[i] = arr->A[i + 1]; // copy an element from A of index plus 1
arr->length--; // after shifting all the elements make the array smaller
return x;
}
return 0;
}
int main()
{
struct Array arr = { {2,3,4,5,6},10,5 };
cout << "You are deleting element " << Delete(&arr, 3) << endl;
Display(arr);
return 0;
}