how can i delete an index from an array

i am trying to make a function that takes a value that is in the array and remove it from the array.

say i have int list1[] = { 1, 5, 3, 10, 4, 5, 10, 1, 2, 10, 10 };

and i want to delete only the first array that has the 10 value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int remove_first(int a[], int n, int del)
{
	int i;
	for (i = 0; i < n; i++)
	{
		if (a[i] == del)
		{
			a[i] = a[i+1];
			break;
		}
	}

	for (i; i < n - 2; i++)
		a[i] = a[i+1];
	return n;
}


i think im trying to shift the number then breaking the loop then trying to shift every number after that with another loop.

but it prints out
1 5 3 4 5 10 1 2 10 10 10 

it puts another ten on it. how do i fix it.
You have a few problems, so I just rewrote it and added a main to test it.

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
30
31
32
33
#include<iostream>
using namespace std;
int remove_first(int a[], int n, int del)
{
	int i;
	for (i = 0; i < n; i++)
	{
		if (a[i] == del)
		{
                       a[i] = 0; //set equal to whatever value you want
                                //to act as the "deleted" value, not the next value
                                //we'll deal with the next value in the following loop
		         break;
		}
	}

	for (; i < n-1; i++) // removed the "i" from the first part, changed to n-1
                                            // n-2 would ignore the last element of the array
		a[i] = a[i+1];
	return n-1; // return n-1, not n
} 

int main()
{
int list[] = {1, 5, 3, 10, 4, 5, 10, 1, 2, 10, 10};
int size = 11;
size = remove_first(list, size, 10);

for (int i = 0; i < size; i++)
    cout << list[i] << " ";

return 0;
}
Last edited on
Nevermind.
Last edited on
What are you doing in those last 2 code blocks? You set i to n and then immediately compare it to n-1. I don't understand the point of your post at all.
Corrected.
Isn't that just removing the element at the "del" position in the array? That's not what OP needs. "del" is the number to be deleted, not the element. For instance, in what I posted, the number 10 is to be deleted, but it first appears in the 3rd element.
how can i delete an index from an array
The title is rather misleading.
You can't really "delete" an item from an array, but you can overwrite it.
The trick is to copy each "next" item to the previous.

Original array values:

    0   1   2   3   4   5
  +---+---+---+---+---+---+
  | 7 | 3 | 8 | 4 | 7 | 1 |
  +---+---+---+---+---+---+

To "delete" the item at index 2:

    0   1   2   3   4   5
  +---+---+---+---+---+---+
  | 7 | 3 | 4 | 4 | 7 | 1 |  copy item at index 3 to 2
  +---+---+---+---+---+---+
            4<--4

    0   1   2   3   4   5
  +---+---+---+---+---+---+
  | 7 | 3 | 4 | 7 | 7 | 1 |  then copy item at index 4 to 3
  +---+---+---+---+---+---+
                7<--7

    0   1   2   3   4   5
  +---+---+---+---+---+---+
  | 7 | 3 | 4 | 7 | 1 | 1 |  and so on
  +---+---+---+---+---+---+
                    1<--1

Once done, you will need to remember the new size of the array.
 - In character arrays, you set the last element to nul ('\0').
 - In other arrays, you typically have an integer variable which is
   the number of elements in the array; you'll have to decrement its
   value.

Final array values:

    0   1   2   3   4   5 
  +---+---+---+---+---+
  | 7 | 3 | 4 | 7 | 1 |     (ignoring element 5)
  +---+---+---+---+---+

Hope this helps.

BTW. An array is a list of "elements" or "items", each element/item has a value.
ok i got it to work and display the right size when its done.

but if i wanted to delete every array with a certain value should i just put the code that deletes one of them in a loop?
Topic archived. No new replies allowed.