Saturday 20 October 2012

Function036

Write a 'C' function to arrange the elements of an integer array in such a way that all the negative elements are before the positive elements. The array is passed to it as an argument.

#include <stdio.h>
#include <conio.h>
void posneg (int    arr[], int    l );
void main ( )
{                  
        int     arr[10], i;
        clrscr ( );
         
        for ( i = 0; i < 10; i++ )
        {                          
                 printf("\n\t\t Enter a number..:"); scanf("%d",&arr[i]);
        }
        printf("\n\n\n\t The array is..: ");
        for ( i = 0; i < 10; i++ )
        {                           
                  printf("%d ",arr[i]);
        }
        posneg ( arr, 10 );
        printf("\n\n\n\t The resultant array is..: ");
        for ( i = 0; i < 10; i++ )
        {                                   
                 printf("%d ",arr[i]);
        }
        getch ( );
}                 
                    
void posneg ( int     arr[], int    l )
{                            
        int     brr[10], j = 0, i;
        for ( i = 0; i < l; i++ )
        {                                  
                 if ( arr[i] < 0 )
                              brr[j++]=arr[i];
        }
        for ( i = 0; i < l; i++ )
        {                                  
                 if ( arr[i] >= 0 )
                              brr[j++]=arr[i];
        }
        for ( i = 0; i < l; i++ )
        {                                      
                 arr[i]=brr[i];
        }
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...