I ran into a problem, I can’t remove two even elements of the array, an error is displayed and the exact stop is called. Please help. Code:

int main() { using namespace std; int size, rez, cheker; cout << "Input the size of the massive"; cin >> size; int*a = new int[size]; for (int i = 0;i <= size;i++) { a[size] = i; cheker = i % 2; if (cheker == 0) { cout << "The even numbers are\n" << a[size]; int*b = new int[size-2]; for (i = 0;i <= size;i += 2) { b[size] = i; } a = b; cout << "The deleted numbers are:" << b[size]; } } system("pause"); return 0; } 
  • And what is the logic here implied? For example, why do we need an array a, if the call goes all the time to one element a [size] - and moreover, an element with such an index does not exist, which causes an error. - MBo
  • a[size] - departure outside the array. b[size] - departure beyond the array. - AnT 2:38
  • What is "remove two even elements"? Even by value or by index? And if two, which two? What if there are more than two? - AnT 2:42
  • It is necessary to remove exactly two values ​​from the array - Roman Skrypnik

1 answer 1

 delete a[0]; delete a[2]; delete a[4]; 

etc. I think clearly

if you want only 2 to delete, then select them and delete

 int i = 0, j = 2;//например, можно любые delete a[i]; delete a[j]; 
  • No, it is not clear. What is it? - AnT
  • deleting even elements of array a - joystick
  • Brilliant. But there they asked for two . - AnT 2:43
  • And I'll fix it right now - joystick
  • What kind of nonsense ... delete is used for a completely different purpose - the release of memory - to pointers. - Mikhailo Nov.