i need help with sorting an array
Hello everyone, im new here...
I am new to C programming, and i really need help, so i hope i can count on the people from this forum.
The problem is - Create an array of n elements (n<20), type in the value for each element, and the program needs to put all the positive elements on the TOP and the negative ones in the bottom, but IN THE SEQUENCE the user typed them in.
Ah yes.....Aditional array is NOT ALLOWED!!
Example:
- input: -5____-4____1____-9____2____3____-10
- output 1____2____3____-5____-4____-9____-10
So far, i made it this far:
Code:
#include <stdio.h>
#define max 20
main()
{
int n[max],i,j,number,save;
do{
printf("NUMBER OF ELEMENTS: \n");
scanf("%d",&number);
if(number<1||number>max) printf("ERROR!!\n");
}while(number<1||number>max);
for(i=0;i<number;i++)
{
do{
printf("%d.ELEMENT VALUE: ",i+1);
scanf("%d",&n[i]);
if(n[i]<-1000000||n[i]>1000000) printf("ERROR!!\n");
}while(n[i]<-1000000||n[i]>1000000);
}
/////////////////////////////////////////////////////////////////
for(i=0;i<number;i++)
{
if(n[i]<0)
{
save=n[i];
for(j=i;j<number;j++) //SECTION OF THE CODE THAT NEEDS EDITING
{
n[j]=n[j+1];
}
n[number-1]=save;
}
}
/////////////////////////////////////////////////////////////////
for(i=0;i<number;i++)
{
printf("\n%d.ELEMENTS: %d",i+1,n[i]);
}
printf("\n\n");
}
I would appreshiate ANY help, please...
And sorry for my english.....
Thanks!