- 1
Debug this function so that it deletes all occurrences of y in array x (whose length is n)
int delete(int *x, int n, int y) { for (int i = 0; i < n; ++i) { if (x[i] == y) { // Found y at position i for (int j = i+1; j < n; ++j) { // Shift data at position // j one position left x[j] = x[j-1]; } --n; // array length is 1 less } } return n; // new array length }
1 Odpowiedź
- 1
int delete(int *x, int n, int y) {
for (int i = 0; i < n; ++i) {
if (x[i] == y) {
// Found y at position i
for (int j = i; j < n-1; ++j) { //MODIFIED LINE
// Shift data at position
// j one position left
x[j] = x[j+1]; //MODIFIED LINE
}
--n; // array length is 1 less
}
}
return n; // new array length
}