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}
#include <iostream>
usingnamespace 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);
}