### Delete a location

Hi , I have to write a function that deletes a location in some array , with its value , for example :
a[0]=2
a[1]=54
a[2]=87
a[3]=76

a[1] should be deleted :
a[0]=2
a[2]=87
a[3]=76

so i wrote this function, but there is something is making it going wrong :

 ``1234567891011121314151617181920212223`` `````` void arr(int a[], int &size) { int value; cout << "Enter location to be deleted: "; cin >> value; for( int i=0; i
Why did you open a new topic for an existing problem? http://www.cplusplus.com/forum/beginner/97575/

it's different problem actually , the past question is deleting a value and shifting the rest values .

here i have to delete the whole location from the array
First off you should check if the given index is valid before entering the loop.

Now as for deleting something from an array, all you need to do is shift everything over one starting at the index to be deleted.

[0][1][2][3][4][5][6][7][8][9]

[3] is deleted, so everything past [3] is moved over to the left 1.

[0][1][2][4][5][6][7][8][9][NULL or 9 still*]

Since you can't really delete out of an array, you're still going to be left with the same number of elements. So if you delete one element, you can either set the last element to null or just leave it as is. Just depends on what you're doing.

Now I don't see you actually writing over anything in this function. So how do you expect anything to be deleted? I just see you decrement size a bunch of times which isn't even close to right.

 ``12345678910111213`` ``````if(idx >= size || idx < 0) return; //index out of bounds if(idx == size - 1) { arr[idx] = null; size--; } else { for(int i = idx; i < size - 1; i++) { arr[i] = arr[i+1]; } }``````

EDIT:
I guess this wasn't your question?
EngineerFatma wrote:
here i have to delete the whole location from the array

That's not possible. Use a linked structure if you need this behavior.
Last edited on
It's the same thing. Remember that arrays are a fixed sized you can't "delete" an element of an array.

it didn't work with me
Last edited on
Well nothing is going to accomplish what you want. The best approximation to emulate the behavior is to copy the entire array into a new location, omitting the to-be-deleted element. But that's silly when you can just do what I posted.
I don't want to delete anything from the array , i just want to decrease the size by one and pass the location without changing anything comes after it , this is confused -_- !
I have no idea what you're wanting. You're going to have to learn to explain this better.
 i just want to decrease the size by one and pass the location without changing anything comes after it
I'd guess: swap the last element with that you want to delete, decrease size and it's gone. It doesn't maintain the order though.

Otherwise do what ResidentBiscuit said and move...
EngineerFatma wrote:
I don't want to delete anything from the array , i just want to decrease the size by one and pass the location without changing anything comes after it ,

When the size of an array is decreased by 1, the very last element is discarded. Hence there are two problems right there:
a) an element which you wanted to keep has been lost
b) the element you wanted to delete is still there

One possibility would be, instead of an array, use a list.
http://www.cplusplus.com/reference/list/list/

The main problem with the list is, it isn't a simple replacement for an array, various other parts of the code would also need to change.
Another option would be to use a std::vector. The vector class allows removal of elements and automatic size reduction when you remove the element.
But removing from a vector is same thing that we've already told OP, that is for some reason unacceptable.
True, but looking at his posts, I doubt that he really knows what he wants.
Topic archived. No new replies allowed.