Debug this function so that it deletes all occurrences of y in array x (whose length is n) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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 }

15th Mar 2021, 3:25 AM
Jayasudha selvan
Jayasudha selvan - avatar
1 Answer
- 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 }
15th Mar 2021, 3:57 AM
deleted