Create deleteElementsInArray.cpp

pull/1193/head
Riya Ranka 2023-05-30 23:30:37 +05:30 committed by GitHub
parent af47764be0
commit 2df272163b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
#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)
cout<<"\nGiven value doesn't found in the Array.";
else { // if same elements value are in array then it would be deleted all irrespective of position in the array
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;
}