0
void add_by_index(int *&arr, int &size, const int value, const int index) {
int *newArray = new int[size + 1];
bool flag = false;
for (int i = 0; i < size; ++i) {
    if (i == index) {
        flag = true;
        newArray[i] = value;
        continue; // << here
    }
    if (flag) {
        newArray[i + 1] = arr[i];
    } else {
        newArray[i] = arr[i];
    }
}
size++;
delete [] arr;
arr = newArray;
}

after I print the array and the result is remains the same with or without "continue"

void showArray(const int *const arr, const int size) {
for (int i = 0; i < size; ++i) {
    std::cout << arr[i] << '\t';
}
std::cout << std::endl;
}

if we have an array with the size of 6, and try to add val by index 4, then when the index is reached we fill newArray(4) with value. continue breaks the code. i = 5, flag = true, so when we reach if (flag) newArray(5 + 1) = arr(5) meaning we omit element arr(4), but when I print, it shows the correct form with no elements omitted.

ko_stray
  • 19
  • 2

0 Answers0