Thread: i need help with sorting an array

  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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you implement a traditional sort function, there are many many examples on the board.

    The only trick is defining a more refined rule for swapping elements
    - if they're both positive, swap according to their order
    - if they're both negative, leave them alone
    - for one of each, make the positive one first

    Create 3 functions to break up your main(), so you can see what is going on
    - input an array
    - print an array
    - sort an array
    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
    Registered User
    Join Date
    Apr 2007
    Location
    Bosnia
    Posts
    5
    i solved the problem, and now i see its easy program actualy
    here it is:
    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    #define max       31
    
    main()
    {
          int n[max],brpoz=0,brneg,element,i,j,broj,save,t;
          
          do{
                printf("UNESI BROJ ELEMENATA NIZA: \n");
                scanf("%d",&broj);
                if(broj<1||broj>max) printf("GRESKA!!\n");
                }while(broj<1||broj>max);
                
                brneg=broj-1;
           ////////////////////////////////////////////////////////////////////////////////////
               for(i=0;i<broj;i++)
               {
                  do{                                 
                       printf("%d.CLAN NIZA: ",i+1);
                       scanf("%d",&element);
                       if(element<-1000000||element>1000000) printf("GRESKA!!\n");
                       }while(element<-1000000||element>1000000);
                  
                  if(element>-1)
                    {
                                n[brpoz]=element;
                                brpoz++;
                    }
                  else if(element<0) 
                    { 
                                n[brneg]=element;
                                brneg--;
                    }
                  
               }
            ////////////////////////////////////////////////////////////////////////////////////   
               
               brpoz++;
               
             for(i=brneg+1,j=broj-1;i<=(brneg+broj)/2;i++,j--)
             {
                           t=n[i];
                           n[i]=n[j];
                           n[j]=t;
             }           
               
               
               
               
             for(i=0;i<broj;i++)
             {
                           printf("\n%d.CLAN: %d",i+1,n[i]);                             
             }
                 
                 printf("\n\n");
             system("pause");    
    }
    sorry, i had no time to translate all the texts from serbian into english, but anyway, you'll get the meaning of the code

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Instead of writing:
    Code:
    if(element<-1000000||element>1000000) printf("GRESKA!!\n");
                       }while(element<-1000000||element>1000000);
    use the minimum and maximum value defined in limits.h:

    INT_MIN Minimum value for an object of type int -32767
    INT_MAX Maximum value for an object of type int 32767

    your code will look like:
    Code:
    if(element<INT_MIN||element>INT_MAX) printf("GRESKA!!\n");
                       }while(element<INT_MIN||element>INT_MAX);
    And finally, I can tell you that this is completely useless since scanf() already truncates a number if its too big. You can't possibly write a number which is too large inside an int and then check if it's too big just to say that you couldn't have possibly done that.
    That condition will never be met.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    May I ask why it's in the window's forum?

    Also don't use void main()!
    Code:
    int main(void) 
    {
    Read the FAQ.

    Also use fgets() and sscanf() otherwise you run the risk of unwanted chars being left in the stream, read the FAQ on user input.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved.

    > #include <iostream.h>
    This isn't a C header file, it's an OLD C++ header file (two reasons not to use it).
    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.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Bosnia
    Posts
    5
    Quote Originally Posted by zacs7 View Post
    May I ask why it's in the window's forum?

    Also don't use void main()!
    Code:
    int main(void) 
    {
    Read the FAQ.

    Also use fgets() and sscanf() otherwise you run the risk of unwanted chars being left in the stream, read the FAQ on user input.
    yes!! I NEVER use int main(void), as you can see in the code I posted

    ps. thanks for your advice, they always come usefull

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Bosnia
    Posts
    5
    Quote Originally Posted by Salem View Post
    Moved.

    > #include <iostream.h>
    This isn't a C header file, it's an OLD C++ header file (two reasons not to use it).
    ...i know. i just used it for testing my code - because of system("pause"). Its just that i had no time to remove unnecesary segments of the code, and to translate some parts like GRESKA (error), UNESI BROJ ELEMENATA (enter the nubmer of elements)....

  9. #9
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Quote Originally Posted by stubin87 View Post
    ...i know. i just used it for testing my code - because of system("pause").
    Isn't system("pause") in stdlib?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    system is standard. The effect of the argument passed to it is system dependent.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not always.
    If command is a null pointer, the system() function shall determine whether the host environment has a command processor.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zacs7 View Post
    Also don't use void main()!
    Code:
    int main(void) 
    {
    He didn't make it void. He simply said "main()," which means it returns an integer.

    And the arguments of main are not void. This does not mean the same thing as empty parentheses. main() is okay, main(void) is not okay.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And the arguments of main are not void. This does not mean the same thing as empty parentheses. main() is okay, main(void) is not okay.
    Err . . . what? For a function declaration, () is the same as (void), in C and C++. For function prototypes, in C, empty parantheses mean "no argument information available", whereas (void) (and either form in C++) means "this function takes no parameters".

    So when prototyping a function that takes no parameters in C, one should use
    Code:
    void function(void);
    and the declaration could be either of
    Code:
    void function(void) {}
    void function() {}
    main() is the same way.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    Err . . . what? For a function declaration, () is the same as (void), in C and C++.
    No it isn't.

    Code:
    void foo()
    {
    }
    
    int main()
    {
        foo(1, 2, 3, 4); /* NO PROBLEM HERE. */
        return 0;
    }
    If this were C++, you'd be right.

    EDIT: I see you're making a distinction between definitions and prototypes. If there is no prior prototype, the definition IS the prototype. The reason that the following works:

    Code:
    void foo();
    void foo(void)
    {
    }
    Is purely for reasons of backward compatibility with old compilers that didn't have full prototype support.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Wow, you're right! Prelude once told me what I told you, but she must have been refering to C++.

    I've learned something today. And now I can correct people whenever they use this.
    Code:
    int main(void) { return 0; }
    Last edited by dwks; 04-18-2007 at 04:41 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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