how to delete index from array

I need to delete an index from the array, but mine is deleting a number rather than an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  else if (input == 6){
        int index=0;
        int count=0;
        cout << "Enter the index to remove:" << endl;
        cin >>index;
        
        for(int i=0; i<cnt; i++)
        {
            if(array[i]==index)
            {
                for(int j=i; j<(index-1); j++)
                {
                    array[j]=array[j+1];
                }
                count++;
                break;
            }
        }
        if(count ==index){
        
            for(int i=0; i<(cnt); i++)
            {
                cout<<array[i]<<" ";
            }}
        else {
            cout<< index <<"is outside the array ";
        }
        
        }
I need to delete an index from the array, but mine is deleting a number rather than an array.


OK, you're mangling the language here so badly that I can't really tell what you want to do.

Firstly, what do you mean by "delete an index"? The index is the number by which you access a given element. In the code you've posted, your elements are numbers, so deleting an element means deleting one of the numbers from your array.

And they you talk about deleting "an array", as if you want to delete the entire array. Do you mean that you want to clear all the elements from the array?

You also haven't shown us the definition of array in your code. Is it a C-style array, or a std::array? Is it a local variable, a global one, or is it dynamically allocated on the heap?

Please can you be more precise about exactly what you want the behaviour of your program to be, and how the behaviour you're actually seeing differs from what you want.

EDIT: Is this a duplicate of http://www.cplusplus.com/forum/beginner/223804/ ? Or are you asking something different?
Last edited on
The logic you're looking for is:

Copy everything from array[index+1] to the end of the array, one element to the left. So array[index] is overwritten with array[index+1], then array[index +1] is overwritten with array[index+2], and so on. Something like:

1
2
3
4
   for(int i=index+1; i<size; i++)
        {
             array[i-1] = array[i];           
        }


Of course, now you have an extra element at the end of the array, so you need to resize the array. Which is awkward, and generally involves making a new array, one element smaller, and copying everything over. Or separately keeping track of the "real" length of the array.

Here's how it's done the C++ way, with a vector:
the_vector.erase(the_vector.begin() + index_to_remove);
All done.

Last edited on
I'm looking to remove a number at index. For example if the array is 12,9,23. Then if the user inputs 2, then 23 will be removed and display 12, 9.
OK, so you're deleting a single element from the array. Repeater's logic will work for that, and their advice to use a std::vector instead is good.
If the array contains three elements, 12,9,23 and you want to remove the last one, nothing complicated is required. Just subtract 1 from the number which holds the array size.
these are the same answers as to your other question.

swap the deleted guy to the last position and decrease the count if not sorted. If sorted, it is repeater's logic or some sort of deleted flag trick.
Im not deleting the last element. It can be at any index; if we look back at the example with 12,9, 23, if the user inputs 1, 9 should be removed. I have a problem with how to delete a number in a specific index without vectors.
The only problem which remains is to translate the sound advice already given, in this thread and the other, into code.
There is a street. (array)
There are people on the street, queuing for Twilight something. (cnt numbers)

A wolf eats Jack. Those, who were after Jack simply take one step forward. The street is as long as it were before, but now there are one less in the queue.

If Jack happened to be the last in the queue, then nobody had to take step forward, but the queue lenght (cnt) would still reduce by one.

If Jack is not in the queue, the queue does not change.


The above logic eats one Jack, the first. If you want all Jacks gone, you can repeat the above until there are no more Jacks in the queue.

(You could look description of std::remove for inspiration, but ...)
if you have 12,9,23 and input 1,
swap [1] and [last] gives you:
12,23,9
delete last gives you
12,23

and the 9 is gone as desired.
Magic :)
Last edited on
and the 9 is gone as desired.
Magic :)

For that special case of removing the middle element from an array of three. But with longer arrays, that's going to mess up the order, which I doubt the OP wants.
when using arrays (not vectors) you can't actually delete an entry, you can only set it to a new value.

that's why all of the shuffling occurs.

imagine the array is written on lined paper, you can't remove a line, you have to shuffle all later entries up a line to fill the gap. leaving a new gap at the bottom, which we can get rid of by "resizing" the array.

--------------
1,2,3,4

removing "2"
3 overwrites 2
4 overwrites 3

the last value still has 4 but is from the "old" version of the list, you will either need to remember where the list ends or resize the list to match the new number of elements.
Last edited on
Topic archived. No new replies allowed.