Question:
How to delete an element from an array in a C program? Urgent Help Needed.(damsel in distress)?
anonymous
2012-05-31 08:18:31 UTC
How to delete an element from an array in a C program…. Kindly explain with example… I need it urgently by tomorrow or my proff going to shoot me. pleaseeeeeeeeeeeeeeeeeeeeeeeeeee Help. 10 points for the best help and thanks in advance for all answerssss…. ppplease help I need it urgentlyyy
And also explain if deleting an element affect the size of an array in any manner. :(
Four answers:
anonymous
2012-05-31 08:43:11 UTC
1st of all deleting an element doesn’t affect the size of array.

compile following C program tht deletes an element from an array (I have not cleared bugs so don’t expect error free compilation)





#include



main()

{

int array[100], position, c9, n9;



printf("Enter num of elements in array\n");

scanf("%d", &n9);



printf("Enter %d elements\n", n9);



for ( c9 = 0 ; c9 < n ; c9++ )

scanf("%d", &array[c9]);



printf("Enter the location where you wish to delete element\n");

scanf("%d", &position);



if ( position >= n9+1 )

printf("deletion not possible.\n");

else

{

for ( c9 = position - 1 ; c9 < n - 1 ; c9++ )

array[c9] = array[c9+1];



printf("Resultant array is\n");



for( c9 = 0 ; c9 < n9 - 1 ; c9++ )

printf("%d\n", array[c9]);

}



return 0;

}





http://itechminds.com/
Jonathan
2012-05-31 13:45:12 UTC
In C, deleting an element of an array depends on the kind of array you are discussing. If elements of the array point to objects that were allocated using malloc(), they must be freed using free(), as well as other mechanics to do it properly. If the elements are value types like "int" or "float" then it may depend on whether or not the order must be preserved. Regardless of that, deleting an element may (or may not) involve changing the size of the array itself if malloc() was used in the first place to create it and if it is desired (or not) to adjust the size when deletion takes place.



By now, you should get the idea that "it depends."



The simplest way to delete an element in a static lifetime array (possesses fixed maximum size that you won't change) which does NOT require order preservation under deletion is to simply copy the last element in the array into the element being deleted and then adjust the active count down by one (after testing to make sure there is at least one active item, of course.) Such code might look like this:



#include

int myarray[]= { 1, 6, 10, 41, 72, 111, 200, 250 };

int mycount= sizeof(myarray)/sizeof(myarray[0]);

int main( void ) {

    int i, itemtodelete= 3;

        printf( "Elements before deletion\n" );

        for ( i= 0; i < mycount; ++i )

            printf( "%d\n", myarray[i] );

        if ( itemtodelete < mycount && itemtodelete >= 0 ) {

            myarray[itemtodelete]= myarray[mycount-1];

            mycount -= 1;

        }

        printf( "Elements after deleting item %d\n", itemtodelete+1 );

        for ( i= 0; i < mycount; ++i )

            printf( "%d\n", myarray[i] );

    return 0;

}



But note that the order is not preserved. Also note that the maximum size of the array does not need to change. Just the count of elements is adjusted.
Philip1234
2012-05-31 08:28:15 UTC
You don't delete the entry you just "erase" it, you can simply set the value to null.



int x[10];

//Fill Array

x[5] = null; //this will delete the entry in the array with index 5.



It does not affect the size since you are just deleting the value of one "slot" in the array. The array length stays intact. However if you allocate memory for the array by using a malloc then when you do a free(array) it will free up all memory given to that array.



Also if you actually want to physically remove an element you will need to keep a count of the number of elements and create a separate array of that size. Then just copy over the values you are not deleting to the new array.
?
2016-10-21 05:46:03 UTC
you study datastructure programming. it totally deals with the above theory. at the same time as ur documents tournament interior the array element then take that array element position from that area to shift each and each of the element one step ahead


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...