-1

I've tried shifting elements backwards but it is not making the array completely empty.

for(i=pos;i<N-count;i++)
        {
            A[i]=A[i+1];
        }

Actually, I've to test for a key value in an input array and if the key value is present in the array then I've to remove it from the array. The loop should be terminated when the array becomes empty. Here "count" represents the number of times before a key value was found and was removed. And, "pos" represents the position of the element to be removed. I think dynamic memory allocation may help but I've not learned it yet.

6
  • 2
    How do you define "empty"? How are you determining that is is not empty? How is A defined? If A is a static array you cannot change its size so there will always be some value in every entry. In that sense you can't "delete" an entry. What you can do is set it to an "invalid" value or keep track of a count of how many of the entries are considered valid. Commented Feb 21, 2022 at 19:42
  • Your question is a little confusing. You say I've to test for a key value in an input array and if the key value is present in the array then I've to remove it from the array but then you also say ` The loop should be terminated when the array becomes empty.` so which is it are you emptying the whole array or deleting one element? This link describes removing elements from an array: stackoverflow.com/questions/15821123/… & this link describes emptying an array: stackoverflow.com/questions/32398192/… Commented Feb 21, 2022 at 19:42
  • 1
    Maybe a linked list would be the data structure you need. You cannot delete elements in the middle of an array, you can just change the values. Commented Feb 21, 2022 at 19:47
  • 1
    for a fixed size array you can use sentinel values, ie ones that you know mean 'end' or 'empty', like 0, or -1. YOu cannot adjust the actual size. Alternately keep a count of valid entries Commented Feb 21, 2022 at 19:49
  • Presumably either N or count records the number of elements currently stored in the array. So, after your loop is done, you need to do either N -= 1 or count -= 1. (Or, if as seems likely, you're actually deleting count number of items at position pos, it'd be N -= count, and you'd also want A[i] = A[i+count] in the loop.) Commented Feb 21, 2022 at 20:13

1 Answer 1

1

From your description and code, by "delete" you probably mean shift the values to remove the given element and shorten the list by reducing the total count.

In your example, pos and count would be/should be the similar (off by 1?) .

The limit for your for loop isn't N - count. It is N - 1

So, you want:

for (i = pos; i < (N - 1); i++) {
    A[i] = A[i + 1];
}
N -= 1;

To do a general delete, given some criteria (a function/macro that matches on element(s) to delete, such as match_for_delete below), you can do the match and delete in a single pass on the array:

int isrc = 0;
int idst = 0;

for (;  isrc < N;  ++isrc) {
    if (match_for_delete(A,isrc,...))
        continue;

    if (isrc > idst)
        A[idst] = A[isrc];

    ++idst;
}

N = idst;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.