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.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
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.