Friday, July 31, 2009

How do you delete the items in an array in c++? (an example would be appreciated.?

These are the 2 possible ways





Sentinel Value :


-------------------------------


Declare a sentinel to indicate if the value at a position is deleted or not.





1. Assume that an array can hold +ve integer values


2. if u want to delete array[10] then


just change its values to a -ve value ( -1 for example )


3. So when traversing the array if u encounter a -ve value it means that the value is deleted.





Shift %26amp; Mark :


-------------------------------


Use a variable to indicate the end of array.





1. Ur Array holds 100 values currently so


Array_End = 99 ( 0 - 99 = 100 )


2. If u want to delete array[31] then


Shift Array[31] = Array[32] = Array[33] ... Array[Array_End];


( i:e u shift the array contents to the left )


3. Update the Array_End to point 1 position to the left


( Array_End --)





These are static ideas of deleting array values.


But to have a dynamic control u need to use the new / delete operator or use a link list as an array.





Hope this helps u in getting an idea.

How do you delete the items in an array in c++? (an example would be appreciated.?
u sud do it in dynamic form ie use pointer

video cards

No comments:

Post a Comment