The purpose of an array is to provide access to the same type of data at multiple memory locations through base address and index. In various applications, arrays are used to store data for various reasons. Arrays must handle adding, deleting, and updating elements as efficiently as other data structures. Both static arrays and dynamic arrays contain a number of library functions that handle various array-related operations in C. But in this article, we will see how to remove elements from an array without using any library function.
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]
Delete elements from any position, there are three possible situations. Delete from the beginning, delete from the end, delete from the middle of any index. Removing from the end does not require any shifting. But the rest of the other two require moving elements to the left. First remove an element from the position and then fill the position with consecutive elements. Let us see the algorithm and C code for clear understanding.
Get the array A with n elements, the position is pos
If pos >= n 1, then
Cannot delete, exit function
otherwise
For index c = pos to n − 1, perform the following operations
A[c]=A[c 1]
Finish
n := n − 1
End if
#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); }
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,
We showed in this article how to remove elements from an array. This is a universal process and we can delete anywhere we like, including the beginning, the end, and the middle. Vectors are not used because we are not using any library functions. For dynamically sized arrays, a vector-based approach is also an option.
The above is the detailed content of C++ program to delete elements from an array without using library functions. For more information, please follow other related articles on the PHP Chinese website!