0

I tried to write a function that gets an object ("Stone") and deletes the stone from a given array. code:

void Pile::del_stone(Stone &s)
{
    Stone *temp = new Stone[size - 1];//allocate new array
    for (int i = 0;i <size;++i)
    {
        if (s != Pile_arr[i])//if the given object to delete is different from the current 
        {
            temp[i] = Pile_arr[i];//copy to the new array
        }
        else
        {
            i--;
        }
    }

    Pile_arr = temp;
    set_size(this->size - 1);
    temp = NULL;
    delete[] temp;
}

Pile_arr is a member of Pile class. The problem is that i get an infinite loop, because i decrease i. I cant figure out how to solve this issue. Any ideas?

1
  • Why are you not using one of the standard containers? Commented Apr 12, 2015 at 11:40

3 Answers 3

2

Use two indexes: i and j. Use i to know which element of the original array you are looking and j to know where to put the element in temp.

Sign up to request clarification or add additional context in comments.

3 Comments

can you give an example?
void Pile::del_stone(Stone &s) { Stone *temp = new Stone[size - 1];//allocate new array for (int i = 0,int j=0;i <size;++i,j++) { if (s != Pile_arr[i])//if the given object to delete is different from the current { temp[j] = Pile_arr[i];//copy to the new array } else { j--; } } Pile_arr = temp; set_size(this->size - 1); temp = NULL; delete[] temp; }
That would be my idea. I haven't tested it, but that should work.
1

You need to use a separate counter to track where new elements should be placed. I have used n below:

Stone *temp = new Stone[size - 1];
int n = 0; // Stores the current size of temp array
for (int i = 0;i <size;++i) {
    if (s != Pile_arr[i]) {
        temp[n++] = Pile_arr[i];
    }
}

It's also worth considering the case where s is not found in the array, as this would cause a runtime error (Attempting to add size elements to an array of size size - 1).

Using a STL container would be a far better option here.

Comments

0

This function will:

  • Allocate a new array of length size-1
  • Search for the intended object
    • If you find it, copy it to the same exact position in the array
    • If you don't --i
    • Finally, ++i

First of all, this function is bad for 3 reasons:

  1. It only copies one item over--the given item. You have an array with only 1 object.
  2. It copies the item from index to index. Since the final array is one smaller, if the object is at the max original index, it will be out of bounds for the new array.
  3. If the object is not immediately found, the array will get stuck, as you decrease the index, and then increase it using the loop--you'll never move again.

    Stone *temp = new Stone[size - 1];//allocate new array for (int i = 0;i

Instead:

  1. Cache the found object, then delete it from the original array or mark it. temp = found object
  2. Copy the array, one by one, without copying empty spaces and closing the gap. Copy temp_array[i] and increment i if and only if temp_array[j] is not marked/deleted. Increment j
  3. Decide where to put the found object.

Once again, you can decide to use separate indexes--one for parsing the original array, and one for filling the new array.

2 Comments

Yes i know, thats why im looking for an answer
@Itay209 Edited for clarity and more in-depth.

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.