another sort useless program

This is a discussion on another sort useless program within the C Programming forums, part of the General Programming Boards category; anyone that wants to call me poopy thats ok ! the problems im having with this useless program is when ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Question another sort useless program

    anyone that wants to call me poopy thats ok !
    the problems im having with this useless program is when the user hits 2 for unsorted its unsorted but after its sorted it remains that way. how can this be fixed? also it suppose to quit with the q but doesnt!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void sort(int *array);
    
    int main(int argc, char *argv[])
    {
    char answer[2] ;
    int num, c;
    int array[12]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4,
                    23,24};
       
      
    printf("%25c----------------\n",0);
    printf("%25c sort or unsort?\n",0);
    printf("%25c----------------\n");
    printf("\n");
    printf("would you like the numbers sorted or unsorted?\n");
    
    printf("type 1 for sorted. 2 for unsorted. q to quit\n");
    
     do{
       gets(answer);
       num = atoi(answer);
    
     switch(num){
       case 1:
        printf("%22c---------Sorted----------\n");
        sort(array);
         for(c = 0; c < 12; c++)
         printf("%i\t",array[c]);
         printf("\n");
         break;
      
       case 2:
        printf("%22c------Unsorted-------\n");
         for(c = 0; c < 12; c++)
         printf("%i\t",array[c]);
         break;
      
      printf("\n");}
    }
    
      while(num != 'q');
       system("pause");	
       return 0;
    }
    
    void sort(int *array)
      {
        int a, b, temp;
        for(a = 0; a < 12; a++)
           for(b = 0; b < 12; b++)
              if(array[a] < array[b])
              {
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
    }
    }

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Here is an example of formated code...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void sort(int *array);
    
    int main(int argc, char *argv[]) {
        
        char answer[2] ;
        int num, c;
        int array[12]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4, 23,24};
           
          
        printf("%25c----------------\n",0);
        printf("%25c sort or unsort?\n",0);
        printf("%25c----------------\n");
        printf("\n");
        printf("would you like the numbers sorted or unsorted?\n");
        printf("type 1 for sorted. 2 for unsorted. q to quit\n");
        
        do {
            gets(answer);
            num = atoi(answer);
            
            switch(num) {
                case 1:
                printf("%22c---------Sorted----------\n");
                sort(array);
                for(c = 0; c < 12; c++) {
                    printf("%i\t",array[c]);
                }
                printf("\n");
                break;
                
                case 2:
                printf("%22c------Unsorted-------\n");
                for(c = 0; c < 12; c++) {
                    printf("%i\t",array[c]);
                }
                break;
                
                printf("\n");
            }
        } while(num != 'q');
        system("pause");    
        return 0;
    }
    
    void sort(int *array) {
        int a, b, temp;
        for(a = 0; a < 12; a++) {
            for(b = 0; b < 12; b++) {
                if(array[a] < array[b]) {
                    temp = array[a];
                    array[a] = array[b];
                    array[b] = temp;
                }
            }
        }
    }
    The problem with trying to print the unsorted version after sorting it is that you are pasing a pointer to the first element in the array to the function... because of that you are modifying the original array. Also you are comparing the number you made from the input to 'q' not the answer you got from the user.

    EDIT:
    dwks: Ran it through a formatter I wrote a while back... but it is really basically just indents on { } since it was for my programing style and I always put { } after any loop even if it is just one thing in it.
    Last edited by Wraithan; 09-16-2006 at 07:14 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    gets(answer);
    Quick, read the FAQ on why not to use gets(). It's worse than fflush(stdin)!

    Code:
    printf("%25c sort or unsort?\n",0);
    Printing the character \0 probably isn't a good idea; try a space. (And did you mean %s?)

    Code:
       case 2:
        printf("%22c------Unsorted-------\n");
         for(c = 0; c < 12; c++)
         printf("%i\t",array[c]);
         break;
      
      printf("\n");}
    That printf, being after the break, will never execute. I'm guessing you meant to put it before the break or outside the switch statement entirely.

    Code:
    system("pause");
    There's another FAQ on this . . . 2 actually:
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    void sort(int *array) {
    	int a, b, temp;
    	for(a = 0; a < 12; a++)
    	for(b = 0; b < 12; b++)
    	if(array[a] < array[b]) {
    		temp = array[a];
    		array[a] = array[b];
    		array[b] = temp;
    	}
    }
    That is formatted? . . . if so, it could be better. (In my opinion at least. But then I don't indent switch labels either.)

    You have lots of unnessesary repeated code in your switch statements. For example, you could print the array outside the switch, and you'd only have to do it once.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Question

    ok i cleaned it up a bit! I tried to send the array without a pointer but it still left it changed! I do remember reading that somewhere that if you pass it as a pointer it changes it for good. but i dont know how to fix this. or how to fix the quit
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void sort(int array[12]);
    
    int main(int argc, char *argv[]){
        char answer[2] ;
        int num, c;
        int array[12]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4, 23,24};
       
      
        printf("%25c----------------\n",0);
        printf("%25c sort or unsort?\n",0);
        printf("%25c----------------\n");
        printf("\n");
        printf("would you like the numbers sorted or unsorted?\n");
    
        printf("type 1 for sorted. 2 for unsorted. q to quit\n");
    
        do{
             fgets(answer,sizeof(answer), stdin);
             num = atoi(answer);
    
             switch(num){
             case 1:
             printf("%22c---------Sorted----------\n");
             sort(array);
             for(c = 0; c < 12; c++)
             printf("%i\t",array[c]);
             printf("\n");
             break;
      
             case 2:
             printf("%22c------Unsorted-------\n");
             for(c = 0; c < 12; c++)
             printf("%i\t",array[c]);
             break;
      
         }
             
      }while(answer != 'q' );
        getchar();	
       return 0;
    }
    
    void sort(int array[12]){
        int a, b, temp;
        for(a = 0; a < 12; a++)
        for(b = 0; b < 12; b++)
        if(array[a] < array[b]){
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
        }
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    }while(answer != 'q' );
    answer is a char array; either use answer[0] or strcmp() or compare num to a number (such as 0) that indicates quit.

    There's nothing wrong with your sorting function that I can see; it's quite similar to these:


    [edit] Wow, no gets(), no fflush(stdin), no system("pause")! There's almost nothing to complain about. [/edit]
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Code:
    printf("%25c----------------\n",0);
    how could i do this the right way? spaces?
    i saw someone do this somewhere and i used it!

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    %s, like I suggested.
    Code:
    printf("%25s", "-----\n");
    %-25s to right-align it, I believe.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Wink

    ok i used answer[0] and it works great. now i understand that 0 is the first element in the array and is that where q will be stored so its looking in that first element?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void sort(int array[12]);
    
    int main(int argc, char *argv[]){
        char answer[2] ;
        int num, c;
        int array[12]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4, 23,24};
       
      
        printf("%25s----------------\n");
        printf("%25s sort or unsort?\n");
        printf("%25----------------\n");
        printf("\n");
        printf("would you like the numbers sorted or unsorted?\n");
    
        printf("type 1 for sorted. 2 for unsorted. q to quit\n");
    
        do{
             fgets(answer,sizeof(answer), stdin);
             num = atoi(answer);
    
             switch(num){
             case 1:
             printf("%22c---------Sorted----------\n");
             sort(array);
             for(c = 0; c < 12; c++)
             printf("%i\t",array[c]);
             printf("\n");
             break;
      
             case 2:
             printf("%22c------Unsorted-------\n");
             for(c = 0; c < 12; c++)
             printf("%i\t",array[c]);
             break;
      
             printf("\n");}
             
      }while(answer[0] != 'q' );
       system("pause");	
       return 0;
    }
    
    void sort(int array[12]){
        int a, b, temp;
        for(a = 0; a < 12; a++)
        for(b = 0; b < 12; b++)
        if(array[a] < array[b]){
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
        }
    }

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Yuppers that is correct.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Yes, exactly. 'q' is a character and answer is a string, so you need to look at answer[0] (as you have done) or use strcmp(), which will allow only "q" and not "quit" (but then your buffer can only hold one non-NULL character anyway, so it doesn't matter). In short: you've done it correctly.

    But why oh why is the system("pause") back in there? You had it out! Are you trying to give me something to complain about?

    This isn't quite right.
    Code:
    printf("%25s----------------\n");
    Use the code I posted:
    Code:
    printf("%25s", "----------------\n");
    [edit] These need to change too:
    Code:
    printf("%22c---------Sorted----------\n");
    ->
    Code:
    printf("%22s", "---------Sorted----------\n");
    [/edit]
    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
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Smile

    sorry about the system pause i had edited the code i posted and i never changed it in devc++ =(

    thanks for all your help!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void sort(int array[12]);
    
    int main(int argc, char *argv[]){
        char answer[2] ;
        int num, c;
        int array[12]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4, 23,24};
       
      
        printf("%40s", "----------------\n");
        printf("%40s", " sort or unsort?\n");
        printf("%40s", "----------------\n");
        printf("\n");
        printf("would you like the numbers sorted or unsorted?\n");
    
        printf("type 1 for sorted. 2 for unsorted. q to quit\n");
    
        do{
             fgets(answer,sizeof(answer), stdin);
             num = atoi(answer);
    
             switch(num){
             case 1:
             printf("%40s", "---------Sorted----------\n");
             sort(array);
             for(c = 0; c < 12; c++)
             printf("%i\t",array[c]);
             printf("\n");
             break;
      
             case 2:
             printf("%40s", "------Unsorted-------\n");
             for(c = 0; c < 12; c++)
             printf("%i\t",array[c]);
             break;
      
             printf("\n");}
             
      }while(answer[0] != 'q' );
       getchar();	
       return 0;
    }
    
    void sort(int array[12]){
        int a, b, temp;
        for(a = 0; a < 12; a++)
        for(b = 0; b < 12; b++)
        if(array[a] < array[b]){
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
        }
    }

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You're welcome. Your program passes inspection, except that instead of hard-coding 12 everywhere, you could pass the size of the array to sort. You can calculate the number of elements in an array as follows (this doesn't work on an "array" that has been passed to a function, a consequence of arrays degrading into pointers that you mentioned earlier):
    Code:
    sizeof(array) / sizeof(*array)
    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
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    except that instead of hard-coding 12 everywhere, you could pass the size of the array to sort
    ok i tried to do this:
    Code:
    size = sizeof(array) / sizeof(*array);
    then pass that size to the function, but it did not work. i dont know why it wont work.
    could i use a #define for the 12?

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You could, but Dave_Sinkula won't like it. Printf size to see if it is 12 or not.

    You can also leave out the sizes of the array in the declaration and passing it to the function.
    Code:
    int array[12]
    void function(int array[12])
    [edit] Then post your code again. [/edit]
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  4. malloc crashing program
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 02-08-2003, 01:52 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21