Thread: i need help with sorting an array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Bosnia
    Posts
    5

    Question 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("&#37;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!
    Last edited by stubin87; 04-15-2007 at 05:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM