answersLogoWhite

0


Best Answer

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

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a function in c plus plus to delete an element from a sorted array so that its sorted order remains as it is?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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 softed by its state in rtp. another way of sorting is element by element.


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).


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.


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 required in order to perform a binary search?

1. direct access to the elements 2. the elements are sorted


Simple c program for selection sort?

The Selection Sort definition is rather simple : find the largest number (element) in a list and move it to it's position in sorted form.You can perform selection sort like, smallest elements are in the beginning and largest element at the end.Now how this element arrange to it's exact position,We can do this by swapping elements at highest index and the process is continue till all the elements are sorted.


How can you find an element?

I'm assuming you mean "how can you find an element on the periodic table?" The periodic table is strategically placed. You can either look for the atomic number (the number at the top-- the number of the protons in one atom), or you can just look for the Element Symbol. The periodic table from left to right is sorted by increasing atomic number. The periodic table from top to bottom is sorted by increasing number of energy levels (the levels in which the electrons are in).


How do you get sorted on harrypottercom?

You cannot get sorted on that website. However, you can be sorted on Pottermore.


Into what 3 categories is paper sorted?

where and how is paper sorted? where and how is paper sorted?


Does the Golgi apparatus have a polarity or sidedness to its structure and function?

Proteins in the membrane of the Golgi may be sorted and modified as they move from one side of the Golgi to the other. and Lipids in the membrane of the Golgi may be sorted and modified as they move from one side of the Golgi to the other.