fork download
  1. #include <iostream>
  2.  
  3. int removeodd(int arr[], int arrsize)
  4. {
  5. int di = 0; // di == destination index
  6. for (int si = 0; si < arrsize; ++si) // si == source index
  7. if (arr[si] % 2 == 0) // even number => copy it
  8. arr[di++] = arr[si]; // assign arr[si] to arr[di], then increment di
  9. return di; // "size" of new array
  10. }
  11.  
  12. int main()
  13. {
  14. int size = 8;
  15. int ary[8] = { 1,2,3,6,7,9,5,8 };
  16.  
  17. int newsize = removeodd(ary, size);
  18. for (int i = 0; i < newsize; ++i)
  19. std::cout << ary[i] << ' ';
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
2 6 8