Check The Programming Section
Delete an item from array
Consider the image displayed above, in which an array have 10 items and size of the array is 10 and the value of count is equal to size of the array. The required steps to delete an item from the array are as follows:
At first, an array with 10 items is displayed and count reflectes number of items present in the array.
The blue arraow indicates the item to be deleted at index 3, which is the 4th element of the array.
The second row shows number of shifting operation required to delete 11 from location 3, which is equal to count - (pos+1) = 6
The last row displays the array after deletion. Most importantly, if we look at the array, the last element is present at position 9 and 10, but count is reduced by 1 to 9.
We need to understand that, when we delete an item from the array in C/C++, the item actually remains in the position. Unless that location is overwriten by another value. Because array gets continous memory blocks from RAM.
One way to print the correct array, by remembering the actual number of items present in the array. For this purpose count variable is used.
Like insertion, we can also delete an item from the array, any of these three position
Delete an item at the begining of the array
Delete an item in between two items, by passing the location and
Delete an item from last
Before delete an item, it is compulsory to check array is empty or not. If array is empty, deletion operation can not be executed. To check this, we can use one global variable count which is initialised to zero. When an item is inserted to array the gloabl variable count is increment by 1 and when an item is deleted from array the count will be decremented by 1.
int count = 0;
int main(){
int num[10];
//after insertion of one item
count++;
//after deletion of one item
count--;
}
Before start deletion of an item, let's declare an array with size 10 and takes some input. The required code is (edit and try):
#include<iostream>
using namespace std;
int count = 0;
int main(){
int num[10];
int size = sizeof num/sizeof num[0];
char choice;
while(count<size){
cout<<"Enter an item ::";
cin>>num[count];
count++;
cout<<"Do you wants to enter another item?y/n ::";
cin>>choice;
if(choice =='y' && count<size)continue;
else if (choice =='y' && count==size){
cout<<"Array is full\n";
break;
}
else break;
}
}
The while loop can execute maximum 10 times for taking input. So the condition count<size will check always, how many items already entered into the array. After entering an item the count is incremented by count++; . The if statement inside while loop gives you an option as ‘y’ for yes and ‘n’ for no for entering an item or not. and consider that we have entered 4 items to the array.
Enter an item ::34
Do you wants to enter another item?y/n ::y
Enter an item ::67
Do you wants to enter another item?y/n ::y
Enter an item ::89
Do you wants to enter another item?y/n ::y
Enter an item :: 23
Do you wants to enter another item?y/n ::n
A display() function is designed to display the array and the array will pass to that function as argument.
void display(int num[]){
for(int i=0;i<count;i++){
cout<<num[i]<<" ";
}
cout<<endl;
}
cout<<"Present status of the array......\n";
display(num);
Delete an item 34 from the array
Suppose we wants to delete the first item 34 of the array. At present array have 4 items so the value of count is 4. The required code to delete the first item of the array is
void delFirst(int num[]){
for(int i=0;i<count-1;i++){
num[i] = num[i+1];
}
count--;
}
The item at index [i] is replaed by item at index [i+1], to next consecutive location.
The shifting operation is performed by the statement num[i] = num[i+1]; inside the for loop.
The number of iteration (i = 0, to 2) of the for loop is equal to the shifting operation as count -1 = 3. The for loop started from i = 0; and till executed to i<count-1;. Because we have shifted the next location value to its previous location, one location in advance by i+1.
After deleting the first item count is reduced by 1 count--;
In this case the number of required shifting operation is equal to count - (given position+1). Consider the image depicted below:
Delete item 89 from position 1
void delAtPos(int num[], int pos){
for(int i=pos;i<count;i++){
num[i] = num[i+1];
}
count--;
}
The shifting operation is started from the given position passed to the function variable pos.
The shifting operation count-(pos+1) is controlled by i<count;. The value of i started from a given position int i=pos;.
After shifting all items, the count is decremented by one count--;.
This is one of the easiest deletion operation in an array. No shifting operation is required, we simply needs to reduce the value of the count by 1, to remember the actual number of values present in the array. The required function can be a inline function, because its simply have one line of code as follows:
inline void delLast(){
count--;
}
#include<iostream>
using namespace std;
int count = 0;
void display(int num[]){
for(int i=0;i<count;i++){
cout<<num[i]<<" ";
}
cout<<endl;
}
void delFirst(int num[]){
for(int i=0;i<count-1;i++){
num[i] = num[i+1];
}
count--;
}
void delAtPos(int num[], int pos){
for(int i=pos;i<count;i++){
num[i] = num[i+1];
}
count--;
}
inline void delLast(){
count--;
}
int main(){
int num[10];
int size = sizeof num/sizeof num[0];
char choice;
int pos;
while(count<size){
cout<<"Enter an item ::";
cin>>num[count];
count++;
cout<<"Do you wants to enter another item?y/n ::";
cin>>choice;
if(choice=='y' && count<size)continue;
else if (choice=='y' && count==size){
cout<<"Array is full\n";
break;
}
else break;
}
cout<<"Present status of the array......\n";
display(num);
cout<<"After deleting first item..... \n";
delFirst(num);
display(num);
cout<<"After deleting last item..... \n";
delLast();
display(num);
cout<<"Enter a position for delete an item....";
cin>>pos;
if(pos<count){
delAtPos(num, pos);
display(num);
}
else{
cout<<"The entered position is not a valid position...\n";
}
}