Program to swap positave and negative numbers in C



#include
#include
void sorting(int z,int a[])
{ int x,y,t;
for (x=z;x>0;x--)
{

for(y=0;y {
if(a[y]>a[y+1])//here we start swapping
{
t=a[y];
a[y]=a[y+1];
a[y+1]=t;
}
}

}
return;
}
int main(void)
{
int x=0,z,a[100],y,t;
printf("Enter the positive numbers to be sorted and press a negative number to end input:\n");
while(a[x-1]>=0)
{
scanf("%i",&a[x]);
x=x+1;
}
z=x-2;
printf("You have entered:\n");
for(x=0;x<=z;x++) printf("%i\n",a[x]);
sorting(z,a);//function call
printf("Sorted array is:\n");
for(x=0;x<=z;x++) printf("%d\n",a[x]);
getch();
return 0;
}




No comments:

Post a Comment