codescracker
c++

C++ Program to Delete an Element from an Array



« Previous Program Next Program »

In this article, you will learn and get code on deleting an element from an array in C++. The program is created in following ways:

Delete Element from Array in C++

To delete an element from an array in C++ programming, you have to ask from user to enter the 10 array elements first. And then ask to enter the element that has to be delete. Now search that number or element and delete it, if found. Otherwise print a message like, element doesn't found

The question is, write a program in C++ that deletes an element from an array. Here is the answer to this question:

// C++ Program to Delete an Element from an Array
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int arr[10], tot=10, i, elem, j, found=0;
    cout<<"Enter 10 Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nEnter Element to Delete: ";
    cin>>elem;
    for(i=0; i<tot; i++)
    {
        if(arr[i]==elem)
        {
            for(j=i; j<(tot-1); j++)
                arr[j] = arr[j+1];
            found++;
            i--;
            tot--;
        }
    }
    if(found==0)
        cout<<"\nElement doesn't found in the Array!";
    else
        cout<<"\nElement Deleted Successfully!";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program delete element from array

Now supply any 10 numbers say 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 as 10 array elements. After entering 10 elements for array arr[], enter the number/element say 3 that has to be delete from the list. Here is the final snapshot of the sample run:

delete element from array c++

Note - If the entered element (that has to be delete) found in repeated order (more than one time), then the element gets deleted from all the position. That is, repeated element also gets deleted.

When user enters 10 array elements as 1, 2, 3, 4, 5 6, 7, 8, 9, 10, then it gets stored in the array arr[] in following way:

And the entered element say 3 (that has to be delete) gets stored in elem. Now the dry run (inside the for loop) of above program goes like:

Note - In above program, if element found at any index say 2 in the array, then from here, all the elements gets moved to one index back. That is, element at 3rd index gets moved to 2nd index, element at 4th index gets moved to 3rd index, and so on

Delete Element and Print New Array

This program has two extra features comparing with previous one. The two extra features are:

The question is, write a program in C++ that deletes an element and prints the new array. The answer to this question is given below:

// Delete Element and Print New Array
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, elem, j, found=0;
    cout<<"Enter the Size: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nEnter Element to Delete: ";
    cin>>elem;
    for(i=0; i<tot; i++)
    {
        if(arr[i]==elem)
        {
            for(j=i; j<(tot-1); j++)
                arr[j] = arr[j+1];
            found=1;
            i--;
            tot--;
        }
    }
    if(found==0)
        cout<<"\nElement doesn't found in the Array!";
    else
    {
        cout<<"\nElement Deleted Successfully!";
        cout<<"\n\nThe New Array is:\n";
        for(i=0; i<tot; i++)
            cout<<arr[i]<<"  ";
    }
    cout<<endl;
    return 0;
}

Here is its sample run with user input

First enter the size as 8 and press ENTER key. Here is the output you will see:

delete element in array c++

Now enter all the 8 elements for the array say 1, 2, 3, 2, 4, 2, 5, 2 and press ENTER key. Here is the output you will see:

remove element from array c++

Now finally enter 2 as element to remove/delete and press ENTER key to delete 2 from the list and print the new array as shown in the snapshot given below:

c++ delete number from array

Delete Element from Array using Function

This program is the complete version of deleting an element from an array program in C++. It produces the output based on the input. Also handles an invalid input by user at run-time. This program uses user-defined function delElem().

The function delElem() takes three argument. The first argument is array, second is the size, and third is the element that has to be delete. To learn more about function in C++, you can refer to its separate tutorial.

// Delete Element using User-defined Function
// ----codescracker.com----

#include<iostream>
using namespace std;
int delElem(int [], int, int);
int main()
{
    int arr[100], tot, i, elem, chk;
    cout<<"Enter the Size: ";
    cin>>tot;
    if(tot>0)
    {
        cout<<"Enter "<<tot<<" Array Elements: ";
        for(i=0; i<tot; i++)
            cin>>arr[i];
        cout<<"\nEnter Element to Delete: ";
        cin>>elem;
        chk = delElem(arr, tot, elem);
        if(chk==101)
            cout<<"\nElement doesn't found in the Array!";
        else if(chk==102)
        {
            cout<<"\nElement Deleted Successfully!";
            cout<<"\n\nThe New Array is:\n";
            cout<<"Empty!";
        }
        else
        {
            cout<<"\nElement Deleted Successfully!";
            cout<<"\n\nThe New Array is:\n";
            for(i=0; i<chk; i++)
                cout<<arr[i]<<"  ";
        }
    }
    else
        cout<<"\nInvalid Input!";
    cout<<endl;
    return 0;
}
int delElem(int arr[], int tot, int elem)
{
    int i, j, found=0;
    for(i=0; i<tot; i++)
    {
        if(arr[i]==elem)
        {
            for(j=i; j<(tot-1); j++)
                arr[j] = arr[j+1];
            found++;
            i--;
            tot--;
        }
    }
    if(found==0)
        return 101;
    else if(tot==0)
        return 102;
    else
        return tot;
}

Here is its sample run with user input, 5 as size, 1, 2, 3, 2, 4 as 5 array elements, and 2 as element to delete:

delete element from array using function c++

Here is another sample run with user input, 5 as size, 2, 2, 2, 2, 2 as 5 array elements and 2 as element to delete:

c++ delete element from array

Here is another sample run with user input, 5 as size, 6, 7, 8, 9, 10 as 5 array elements, and 3 as element to delete:

c++ remove element from array

And if user enters input, that is not a valid number for above program. For example, a, -4, 0, # etc. Then program will produce the output like Invalid Input!.

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker