answersLogoWhite

0

C++ code example using a vector object (dynamic array):

#include<iostream>

#include<vector>

using namespace std;

typedef std::vector<int> vec;

typedef std::vector<int>::iterator it;

void init_array(vec& v)

{

v.clear();

for(int i=1; i<=10; ++i)

v.push_back(i);

}

void print_array(vec& v)

{

for(it i=v.begin(); i!=v.end(); ++i)

cout<<*i<<" ";

cout<<endl;

}

it get_element(vec& v, unsigned int index)

{

it i=v.begin();

while(index--)

++i;

return(i);

}

int main()

{

vec v;

init_array(v);

cout<<"Initial array:\t";

print_array(v);

// Remove element at index 3 (the 4th element).

v.erase( get_element(v, 3));

cout<<"Modified array:\t";

print_array(v);

return(0);

}

Since Java is object-oriented, you can use a similar approach to the above. However, with C, there's a bit more work involved in managing the memory allocation, however the overall approach is the same:

#include<iostream>

using namespace std;

int* init_array(unsigned int len)

{

int* p=(int*)malloc(len*sizeof(int));

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

p[i]=i+1;

return(p);

}

void print_array(int* p, unsigned int len)

{

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

cout<<p[i]<<" ";

cout<<endl;

}

int main()

{

unsigned int size;

int* p;

size=10;

p=init_array(size);

cout<<"Initial array:\t";

print_array(p,size);

// Remove element at index 3 (the 4th element)

--size;

for( unsigned int y=3; y<size; ++y)

p[y]=p[y+1];

p=(int*)realloc(p,size*sizeof(int));

cout<<"Modified array:\t";

print_array(p,size);

delete [] p;

return(0);

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Will an array element be deleted when you retrieve it from the array?

You cannot delete from an array.


Write an Algorithm to delete a last element from the array?

// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;


Delete an element from an options array?

Set it to null


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;


How delete an array of object within the object in C?

First locate the position of an array by search after than use a delete function to delete an array


How do you get rid of an array in Excel?

Select the entire array and press the Delete key. There are usually only problems if you want to delete some, but not all of the cells in the array.


What is the algorithm using c to delete duplicate elements from an array?

To detect the duplicate, you will have to write a nested loop that compares each element with all the previous elements.To actually delete the duplicate, once you find it, you have to move over all the elements after the duplicate. If the order of the elements doesn't matter, it is faster to just move the LAST array element, overwriting the duplicate element. Use a variable to keep track how many elements of the array are "usable". For example, if your array had 10 elements, and you delete 1, the array size will still be 10... but (after moving the elements over) only 9 of those elements have useful information.


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.


Can you delete array?

If the array was allocated with new, then delete it with delete []. Otherwise, if it was allocated with malloc() then delete it with free. Otherwise, you cannot delete it because it was pre-allocated at link-load time by the compiler.


How much time does it take to delete an array value?

C compiler delete only those memory which is dynamically created. So we can't delete the value of array.


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.


Adavantage of pointer based of a list over an array?

One advantage of a linked list (with pointers) is that it is fairly cheap to insert or delete an element - once you know where it is. A disadvantage is that getting a specific element - for example, the 1000th. element - is expensive.