Tuesday 4 September 2012

Array024

Input some elements in the array and rearrange the array by putting -ve elements before the positive elements.

#include <stdio.h>
#include <conio.h>
void main ( )
{
            int      arr[10], aux[10], i, j;
            clrscr ( );
            for ( i = 0; i < 10; i++ )
            {                                      
                            printf("\n\t\t\t Enter a number..:");    scanf("%d",&arr[i]);
            }
               
            j = 0;
            for ( i = 0; i < 10; i++)
            {                                    
                            if ( arr[i] < 0 )
                            {                                      
                                          aux[j++] = arr[i];
                            }
            }                                 
            for ( i = 0; i < 10; i++ )
            {                          
                            if ( arr[i] >= 0 )
                            {                        
                                         aux[j++] = arr[i];
                            }                
            }                              
            for ( i = 0; i < 10; i++ )
            {                                     
                            arr[i] = aux[i];
            }                                       
                                                   
            printf("\n Rearranged array..:");
            for ( i = 0; i < 10; i++ )
            {                                   
                            printf("%d,",arr[i]);
            }
            getch ( );
}

Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...