Problem: I want the array A[6] = {6, 5, 4, 3, 2, 1}
be A[6] = {5, 3, 1, 1, 1, 1}
. In other words - “delete” every second value, starting from the 0th and shift all other values to the left.
My attempt:
For this, I would use this code, where the length of the corresponding part of the array A (the part with the elements that are not deleted), is the ind-index value that I want to delete.
for (int j = ind; j < n; j++) A[j] = A[j+1];
However, I could not get this to work using code like this:
void deleting(int A[], int& a, int ind){ for (int j = ind; j < a; j++) A[j] = A[j+1]; a--; } int A[6] = {6, 5, 4, 3, 2, 1}; a = 6 for (int i = 0; i < a; i+=2) deleting(A, a, i);
After running this code, I got A[6] = {5, 4, 2, 1, 1507485184, 1507485184}
. So he deleted the items at indices 0, 3. Why did he delete the third index?