Thread: programming problem

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    programming problem

    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 */
    Last edited by Antigloss; 06-02-2005 at 10:53 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #include <malloc.h>
    malloc is prototyped by stdlib.h

    > arr = (int *)malloc( j*sizeof(int) );/* allocate memory for arr */
    malloc doesn't need casting in C, see the FAQ

    > if ( ( ch = getchar() ) != '\n' && ch != EOF )
    If the next char on the line is a \n, then this code throws away an entire line of input.
    The while loop is sufficient, without a guarding if statement as well.

    > Any better solution to deal with this problem?
    Sounds just like sorting to me
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Thanks Salem. You taught me a lot~ ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM