In this tutorial you will learn about the C++ Program to Delete Array Element and its application with practical example.
C++ Program to Delete Array Element
In this tutorial, we will learn to create a C++ program that will delete the element of Array using C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- For loop in C++ Programming.
Program for Delete the element of Array:-
An array is a collection of similar data type elements. In today’s program, we will take an array in input from the user and then we will take the element for deleting from the array. After deleting the element from the array we will reprint the array.
Algorithm for program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Step 1. Initialize the required variable for the program. Step 2. Take size of array in input from the user. Step 3. Taking the elements of the array from the user. Step 4. Taking the location of the deleting element. Step 5. Deleting the element from the array. Step 6. Print the resultant array on the screen. Step 7. End program. |
Program code:-
To Delete an element from the array
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 |
#include<iostream> using namespace std; int main() { //declaring the variables for the program int arr[10], tot=10, i, elem, j, found=0; //Taking input from the user cout<<"Enter 10 Array Elements: "; for(i=0; i<tot; i++) cin>>arr[i]; //taking deletion element from the user 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) //printing the deletion message for the element cout<<"\nElement doesn't found in the Array!"; else cout<<"\nElement Deleted Successfully!"; cout<<endl; return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- arr[10] = it will hold the elements in an array.
- tot = it will hold the number of elements in an array.
- i = it will hold the integer value to control the array.
- Elem = it will hold the location for delete element size of the array.
Taking input from the user in an array of elements.
Deleting the array element.
Printing the Program Code