answersLogoWhite

0

To delete an element form an array you simply move everything one place to the left, starting with the element to the right of the element you wish to remove, and then delete the final element by resizing the array. The following code demonstrates this:

#include <iostream>

void DeleteElement(unsigned int** pp,unsigned int& size,unsigned int index)

{

if(size)

{

unsigned int* p=*pp+index;

unsigned int* c=p+1;

std::cout<<"Deleting element index "<<index<<" with value "<<*p<<" from an array of size "<<size<<std::endl;

--size;

while(index++<size)

*p++=*c++;

*pp=(unsigned int*)realloc(*pp,size*sizeof(unsigned int));

}

}

void PrintArray(unsigned int* a,const unsigned int size)

{

std::cout<<"Array: size="<<size<<" { ";

unsigned int i=0;

while(i<size)

std::cout<<a[i++]<<" ";

std::cout<<"} "<<std::endl;

}

int main()

{

// Allocate a dynamic array of size 5:

unsigned int size=5;

unsigned int* a=(unsigned int*)malloc(size*sizeof(unsigned int));

// Initialise array with sorted values (ascending order):

for(unsigned int i=0;i<size;++i)

a[i]=(i+1)*2;

// Print current array:

PrintArray(a,size);

// Delete element 1 (value 2)

DeleteElement(&a,size,1);

// Print current array:

PrintArray(a,size);

// Delete element 2 (value 4)

DeleteElement(&a,size,2);

// Print current array:

PrintArray(a,size);

// Release allocation.

delete[]a, a=NULL;

return(0);

}

Note that static arrays cannot be resized, so to remove an element you need to maintain a count of the active elements, and move inactive elements to the end of the array. The process is similar to the above, except you need to copy the inactive element's value before shifting everything to the left, and then replace the final element's value with the one you saved.

In C++, operations such as this are greatly simplified by using a vector rather than an array. Vectors are effectively the same as dynamic arrays, but are implemented as self-contained objects with member methods to manipulate the contents, thus greatly simplifying your code. Deleting an element is a simple as calling the vector's built-in delete method. Memory management is largely transparent and, because a vector knows its internal size, passing vectors to functions is made that much easier -- you simply pass them by reference.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Program in c to delete an element in sorted array?

You cannot delete elements from an array. But you can move the elements: if (del_index &lt; no_of_elements-1) { memmove (&amp;array [del_index], &amp;array [del_index+1], sizeof (array [0]) * (no_of_elements - del_index - 1)); } --no_of_elements;


What can matter be sorted by?

Matter can be sorted by physical properties such as size, density, shape, and color. It can also be sorted by chemical properties such as composition, reactivity, and solubility. Additionally, matter can be sorted based on its state of matter, such as solid, liquid, or gas.


What are the advantages and disadvantages of searching in C programming?

If the data is sorted and every element is directly accessible, then you can perform binary search (see built-in function bsearch), otherwise you have to do linear search (which is slower).


What is the value of the kth smallest element in the given array?

The value of the kth smallest element in the array is the kth element when the array is sorted in ascending order.


Program in c plus plus to delete an element in sorted array?

If the array is dynamic then use a vector instead of an array. You can then use the vector::erase() function to delete an element or a range of elements. Remember that if the vector contains pointers to unshared memory, then you must release the pointer before erasing the element containing that pointer. If the array is static then you cannot delete elements. The assumption with static arrays is that you will neither add nor delete, you will only modify existing elements. However, you can emulate a deletion by shunting elements to the left, and keeping track of how many used elements there are (which must always be less than or equal to the upper bound plus one). Again, if the array contains pointers to unshared memory, you must release the pointer before shunting elements. You can also do the same thing with dynamic C-style arrays, but once you've shunted elements to the left you can reallocate the array with the new size to physically delete the final element.


What is the time complexity of an algorithm that uses a binary search to find an element in a sorted array in logn time?

The time complexity of an algorithm that uses binary search to find an element in a sorted array in logn time is O(log n).


How many comparisons are typically made in a binary search algorithm when searching for a specific element in a sorted array?

In a binary search algorithm, typically log(n) comparisons are made when searching for a specific element in a sorted array, where n is the number of elements in the array.


How many comparisons are typically required in a binary search algorithm to find a specific element in a sorted array?

In a binary search algorithm, typically log(n) comparisons are required to find a specific element in a sorted array, where n is the number of elements in the array.


What is the maximum number of comparisons required in a binary search algorithm to find a specific element in a sorted array?

The maximum number of comparisons required in a binary search algorithm to find a specific element in a sorted array is log(n), where n is the number of elements in the array.


Which key element will be best for merge sort and why?

There is no key element in a merge sort. Unlike quick sort which requires a key element (a pivot) to recursively divide a subset into two subsets, merge sort simply divides a subset into subsets of 1 element and merges adjacent subsets together to produce sorted subsets. When there is only one subset remaining, the subset is fully sorted.


What is the code for sorting in C plus plus?

There are many sorting algorithms. One of the simplest to implement is the insertion sort. The following template function will sort an array of any type T that supports the less-than operator. It works by splitting the array into two subsets, where the left portion is sorted and the right is unsorted. Initially, the sorted portion has just one element since a set of one element can always be regarded as being sorted. We then work our way through each of the unsorted elements, from left to right, inserting them into their correct place in the sorted portion. To do this we need to store the current unsorted value thus creating a gap at the beginning of the unsorted portion. This gap then becomes the last element of the sorted portion, reducing the unsorted portion by one element. We then work our way through the sorted elements starting with the element to the left of the gap. If the stored value is less than the current element's value then we copy that element into the gap, thus moving the gap one position to the left. We continue in this manner until gap is at index 0 or the stored value is not less than the element to the left of the gap. We then place the stored value in the gap. We repeat this for all unsorted elements until there are none left, at which point the array is completely sorted. template&lt;typename T&gt; void sort(std::vector&lt;T&gt;&amp; v) { if( v.size()&gt;1 ) { for( size_t i=1; i&lt;v.size(); ++i ) { T t = v[i]; size_t gap=i; while( gap &amp;&amp; t&lt;v[gap-1] ) v[gap]=v[gap--]; v[gap]=t; } } }


What is the binary search definition in computer science and how is it used to efficiently locate a specific element in a sorted array?

Binary search is a search algorithm in computer science that efficiently finds the position of a specific element in a sorted array by repeatedly dividing the search interval in half. This method is used to quickly locate the desired element by comparing it to the middle element of the array and eliminating half of the remaining elements each time, until the target element is found or determined to be absent.