text: Move all zeroes to end of array | Set | C ++ implementation to move all zeroes at the end of array ; function to move all zeroes at the end of array ; Count of non - zero elements ; Traverse the array . If arr [ i ] is non - zero , then swap the element at index ' count ' with the element at index ' i ' ; function to print the array elements ; Driver program to test above
code: #include <iostream> NEW_LINE using namespace std ; void moveZerosToEnd ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 0 ) swap ( arr [ count ++ ] , arr [ i ] ) ; } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 0 , 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Original ▁ array : ▁ " ; printArray ( arr , n ) ; moveZerosToEnd ( arr , n ) ; cout << " Modified array : " printArray ( arr , n ) ; return 0 ; }