-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteElementsInArray.cpp
43 lines (41 loc) · 1.28 KB
/
deleteElementsInArray.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
// Problem: Delete an element in the array given by user, if the element is repetitive then delete all the duplicates of the element in the array.
#include <iostream>
using namespace std;
int main() {
int size, arr[100], i;
cout << "Enter size of array: ";
cin >> size;
for (i=0; i<size; i++) {
cout << "Enter value of array at " << i << " index : ";
cin >> arr[i];
}
cout << endl;
cout << "Your array is ";
for (i=0; i<size; i++) {
cout << arr[i] << " ";
}
cout << endl;
int deleteNum, n=0, j, newSize;
cout << "Enter the value you want to delete in the array: ";
cin >> deleteNum;
for (i=0; i<size; i++) {
if (deleteNum == arr[i]) {
for(j=i; j<size; j++) {
arr[j] = arr[j+1];
}
n++;
i--;
size--;
}
}
if(j == size && n==0) // if same elements value are in array then it would be deleted all irrespective of position in the array
cout<<"\nGiven value doesn't found in the Array.";
else {
cout<<"\n'" << deleteNum << "' gets deleted from the array \n" << "The New Array: [";
for (j=0; j<size; j++) {
cout << arr[j] << " ";
}
}
cout << "]" << endl;
return 0;
}