Write a program to build up an array consist of N integers, then move the integers less than 0 to the left-hand-side of the array, move the integers larger than 0 to the right-hand-side of the array, and left the integers equal to 0 stay at the middle of the array.

Here is my code. Any better solution to deal with this problem?

Please give some advice. Thank you in advance.

Code:#include <stdio.h> #include <malloc.h> void change_pos(int, int, int *, int *);/* move integers */ int input( int * );/* return 1 if the input is legal, otherwise return 0 */ int main() { int *arr; int i = 0, j = 0,/* size of the array */ l = 0; while ( 1 ) { printf("How many integers do you want to input: "); if ( input( &j ) ) { break; } else { printf( "Invalid input!\n" ); } } arr = (int *)malloc( j*sizeof(int) );/* allocate memory for arr */ if (!arr) { printf("Not Enough Memory!\n"); return 1; } for (i=0; i<j; i++) { /* initialize arr */ while ( 1 ) { printf("arr[%d] = ", i); if ( input( arr+i ) ) { break; } else { printf( "Invalid input!\n" ); } } } /* move integers less than 0 to the left-hand-side of the array */ change_pos(0, j, arr, &l); /* move integers less than 1 to the middle of the array */ change_pos(1, j, arr, &l); /* print the moved array */ for (i=0; i<j; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } /* definition of change_pos() starts here */ void change_pos(int n, int j, int *arr, int *l) { int i = *l, k = 0, tmp = 0; for (; i<j; i++) { if (arr[i]<n) { tmp=arr[i]; for (k=i; k>*l; k--) arr[k]=arr[k-1]; arr[k]=tmp; (*l)++; } } } /* definition of change_pos() ends here */ /* definition of input() starts here */ int input( int *arg ) { int ch; int return_val = 0; // used as a return value if ( scanf( "%d", arg ) == 1 ) { // assign return_val with true if integer is inputed return_val = 1; } if ( ( ch = getchar() ) != '\n' && ch != EOF ) { // eliminate garbage in stdin while ( ( ch = getchar() ) != '\n' && ch != EOF ) { ; } } return return_val; } /* definition of input() ends here */