I am trying to remove the negative numbers from array with the following code. Unfortunately, not getting the results. It just prints the first element over and over. Can someone please let me know where am I going wrong?
#include <stdio.h>
void removenegative(int a[],int *p, int *q);
int main()
{
int a[] = {2, 3, -5, -7, 6, 9};
int i;
int *p, *q;
p = a;
q = a+6-1;
removenegative(a, p,q);
for(i=0;i<6;i++)
{
printf("%2d", *p);
}
printf("\n");
}
void removenegative(int a[],int *p, int *q)
{
int *x;
x= &a[0];
while (p<=q)
{
if (*p>=0)
{
*x = *p;
x++;
}
p++;
}
for( ; x<=q; x++)
{
*x = -1;
}
}