-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteVector.cpp
More file actions
50 lines (46 loc) · 1.04 KB
/
deleteVector.cpp
File metadata and controls
50 lines (46 loc) · 1.04 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
46
47
48
49
50
#include <iostream>
using namespace std;
void display(int[], int);
void deleteElem(int[], int, int);
int main()
{
int size = -1;
int* vec = nullptr;
int pos;
do
{
cout << "Enter the size of the vector: ";
cin >> size;
} while (size < 0);
vec = new int[size];
for(int i=0;i<size;i++)
{
cout << "Value " << i+1 << ": ";
cin >> vec[i];
cin.ignore();
}
cout << "Here are your vector" << endl;
display(vec, size);
do
{
cout << "\n Enter position of vector to delete value from the vector: ";
cin >> pos;
} while (pos<0 || pos >= size);
cout << "Deleting...." << endl;
deleteElem(vec, size, pos);
cout << "Here is your new vector after deleting position " << pos << endl;
display(vec, size-1);
delete[] vec;
}
void display(int tab[], int n)
{
for(int j=0;j<n;j++) cout << tab[j] << " ";
}
void deleteElem(int tab[], int n, int position)
{
for(int i=position;i<n;i++)
{
tab[i] = tab[i+1];
}
tab[n]= 0;
}