75. C Program To Separate Zeros from the Given Array Elements.
How does this program work?
- This Program is used to separate zeros from given array elements using C language.
Here is the code
#include <stdio.h>
void reorder (int A[], int n)
{
// k stores index of next available position
int k = 0;
// Do for each element
for (int i = 0; i < n; i++)
{
// If current element is non-zero, put the element at
// Next free position in the array
if (A[i] != 0)
A[k++] = A[i];
}
// Move all 0's to the end of the array (remaining indices)
for (int i = k; i < n; i++)
A[i] = 0;
}
// Move all zeros present in the array to the end
int main(void)
{
int A[] = { 6, 0, 8, 2, 3, 0, 4, 0, 1 };
int n = sizeof(A) / sizeof(A[0]);
reorder(A, n);
for (int i = 0; i < n; i++)
printf("%d ", A[i]);
return 0;
}