fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. const int n = 5;
  6. int arr[n]={0, 9, 0, 8, 1};
  7. int j=0;
  8. for (int i = 0; i < n; i++) {
  9. if (arr[i] != 0) {
  10. int temp = arr[i];
  11. arr[i] = arr[j];
  12. arr[j] = temp;
  13. j++;
  14. }
  15. }
  16. for (int i = 0; i < n; i++) {
  17. printf("%d ", arr[i]);
  18. }
  19. return 0;
  20. }
Success #stdin #stdout 0.01s 5360KB
stdin
Standard input is empty
stdout
9 8 1 0 0