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;
}
|