
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to delete an item from the array without using the library function
The purpose of an array is to provide access to the same type of data across several memory locations via base addresses and indexes. In a wide variety of applications, arrays are used to store data for a wide range of reasons. The array must efficiently handle adding, removing, and updating elements, just like other data structures. Both static and dynamic arrays include a number of library functions in C++ that handle various array-related operations. But in this article, we will see how to delete an element from an array without using any library functions in C++.
Understanding the concept with examples
Given array A = [89, 12, 32, 74, 14, 69, 45, 12, 99, 85, 63, 32] After deleting an element from index 5, the array will be like this: A = [89, 12, 32, 74, 14, 45, 12, 99, 85, 63, 32]
Deleting an element from any position, there are three possible cases. Deleting from the beginning, deleting from the end, and deleting from the middle of any index. Deleting from the end does not require any shifting. But the rest of the other two requires shifting elements towards the left. Firstly remove an element from that location then fill that place with successive elements. Let us see the algorithm and C++ code for a clear understanding.
Algorithm
Get the array A with n elements, the position pos
if pos >= n + 1, then
unable to delete, exit from the function
otherwise
for index c = pos to n − 1, do
A[ c ] = A[ c + 1 ]
end for
n := n − 1
end if
Example
#include <iostream> #include <algorithm> # define Z 30 using namespace std; void displayArr(int arr[], int n ) { for( int i = 0; i < n; i++ ){ cout << arr[ i ] << ", "; } cout << endl; } void deleteElement( int A[], int &n, int pos ){ if ( pos >= n + 1 ) { cout << "Deletion not possible" << endl; return; } else { for ( int c = pos; c < n ; c++ ) { A[ c ] = A[ c + 1 ]; } n = n - 1; } } int main() { int arr[ Z ] = {84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20}; int n = 15; cout << "Given array elements: "; displayArr( arr, n); cout << "Delete from last position (position 15)" << endl; deleteElement( arr, n, 15 ); cout << "Array after deleting last element: " << endl; displayArr( arr, n); cout << "Delete from first position (position 0)" << endl; deleteElement( arr, n, 0 ); cout << "Array after deleting first element: " << endl; displayArr( arr, n); cout << "Delete from position 7" << endl; deleteElement( arr, n, 7 ); cout << "Array after deleting element from index 7: " << endl; displayArr( arr, n); }
Output
Given array elements: 84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20, Delete from last position (position 15) Array after deleting last element: 84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, Delete from first position (position 0) Array after deleting first element: 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, Delete from position 7 Array after deleting element from index 7: 56, 21, 32, 74, 96, 85, 41, 94, 20, 37, 36, 75,
Conclusion
We have shown how to remove an element from an array in this article. This is a generic process that we may remove from anywhere we like, including the beginning, end, and middle. The vectors are not used because we are not using any library functions. For dynamic−sized arrays, vector-based methods are also an option.
- Related Articles
- Golang Program to delete an item from the array without using the library function
- Golang Program to insert an item into the array without using library function
- PHP program to delete an element from the array using the unset function
- Write a C program to Reverse a string without using a library function
- C++ Program to get the first item from the array
- C++ Program to get the last item from the array
- C program to delete an array element using Pointers
- Golang Program to merge two integer arrays without using library function
- Python Program to Calculate the Length of a String Without Using a Library Function
- C Program to delete the duplicate elements in an array
- Golang Program to search an item into the array using interpolation search
- Delete an element from array using two traversals and one traversal in C++ program
- Haskell Program to return an array from the function
- Haskell Program to find the GCD using library function
- Haskell Program to find the LCM using library function
