Move zeros to the end of the array

This problem must move all the zeros found in an array to the end of it.
for ex : array a={12,0,0,-3,-8,0}
after the program executed it needs to output a={12,-3,-8,0,0,0}.

This is my algorithm. How do i fix it as it currently outputs a={12,0,-3,-8,0,0}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
using namespace std;
void nule(int a[100], int n)
{
    int i, aux;
    for(i=0;i<n;i++){
        if(a[i]==0){
            aux=a[i];
            a[i]=a[i+1];
            a[i+1]=aux;
        }
    }
}
void citire(int a[100], int &n)
{
    int i;
    for(i=1;i<=n;i++)
        cin>>a[i];
}
void afisare(int a[100], int n)
{
    int i;
    for(i=1;i<=n;i++)
        cout<<a[i]<<" ";
}
int main()
{
    int a[100], n;
    cin>>n;
    citire(a,n);
    nule(a,n);
    afisare(a,n);
}
closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

void nule(int a[100], int n)
{
    for(int i=0;i<n;i++)
    {
        if(a[i] == 0)
        {
            for(int j=i;j<n-1;j++)
            {
                a[j] = a[j+1];
            }

            a[n-1] = 0;
        }
    }
}

void citire(int a[100], int n)/// No need for "int &n"
{
    for(int i=0;i<n;i++)// You should initial i to 0 otherwise you skipp the first element of "a" array
        cin >> a[i];
}

Hope that helps

void afisare(int a[100], int n)
{
    for(int i=0;i<n;i++)
        cout << a[i] << " ";
}

int main()
{
    int a[100], n;
    cin >> n;
    citire(a,n);
    nule(a,n);
    afisare(a,n);
}
Thank you so much!
Topic archived. No new replies allowed.