Hello! Help, please, write the program code to delete all even elements in the array. Here is the program code itself:

#include <vcl.h> #include <iostream.h> #include <conio.h> #include <stdlib.h> char* Rus(const char* text); void main() { int a[100]; int i,n; randomize(); cout << Rus(" Введите n: "); cin >> n; for (i=0; i<n; i++) a[i]=random(100); for (i=0; i<n; i++) cout<<a[i]<<" "; getch(); } char bufRus[256]; char* Rus(const char* text) { CharToOem(text,bufRus); return bufRus } 
  • 2
    What do you mean by deleting an array element? The balance of the array is shifted to the left and the size of the array is reduced by 1? - avp
  • @ Mary-Angel, To format the code, select it with the mouse and click on the button 101010 of the editor. - angry

1 answer 1

What do you mean by the term "even elements"? Even index or even number? If the first, then:

 for(int i=0; i<n/2; i++) a[i]=a[i*2+1]; for(int i=0; i<n/2; i++) cout << a[i] << ' '; 

If the second, then:

 int newsize=0; for(int i=0; i<n; i++) if(a[i]%2!=0) a[newsize++]=a[i]; for(int i=0; i<newsize; i++) cout << a[i] << " ";