Thread: another sort useless program

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

    Smile

    it still stores the array sorted. can i store the size as a global variable so i can use it in other functions? is that what i should do? and if so how?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void sort(int array[]);
    
    int main(int argc, char *argv[]){
        char answer[2] ;
        int num, c, size;
        int array[]={ 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{
             num = atoi(fgets(answer,sizeof(answer), stdin));
             //num = atoi(answer);
             size = sizeof(array) / sizeof(*array);
             
             switch(num){
             case 1:
             printf("%40s", "---------Sorted----------\n");
             sort(array);
             for(c = 0; c < size; c++)
             printf("%i\t",array[c]);
             printf("\n");
             break;
      
             case 2:
             printf("%40s", "------Unsorted-------\n");
             for(c = 0; c < size; c++)
             printf("%i\t",array[c]);
             printf("\n");
             break;
      }
             
      }while(answer[0] != 'q' );
       getchar();	
       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;
        }
    }
    Last edited by mrsirpoopsalot; 09-17-2006 at 01:24 AM.

  2. #17
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by mrsirpoopsalot
    can i store the size as a global variable so i can use it in other functions? is that what i should do? and if so how?

    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;
        }
    }
    Not necessarily global but local to main.

    This code is quite inefficient.

    The first iteration of the inner loop will be comparing array[0] against array[0] however the lowest value in the array will end up in array[0] at the end of the first iteration of the outer loop.

    The second iteration of the outer loop will be comparing array[1] against array[0] followed by array[1] against array[1], neither of which are useful.

    How about this, where you pass in the size of the array, limit the outer loop to checking up to the penultimate element, and start the inner loop from the next element after that which the outer loop is processing:
    Code:
    void sort(int array[], int elements){
        int a, b, temp;
    
        for(a = 0; a < elements - 1; a++)
           for(b = a + 1; b < elements; b++)
              if(array[a] < array[b]){
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
              }
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not necessarily global but local to main.
    To expand on this, you could initialize the array in main() with no size in it. Then you could declare the size variable which is initialized to sizeof(array)/sizeof(*array). (You only have to do this once since the size of array isn't going to change.) You could then pass size to any functions that use 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.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    To expand on this, you could initialize the array in main() with no size in it. Then you could declare the size variable which is initialized to sizeof(array)/sizeof(*array). (You only have to do this once since the size of array isn't going to change.) You could then pass size to any functions that use array
    Im a little confused about this. did i already do that?
    Code:
    int main(int argc, char *argv[]){
        char answer[2] ;
        int num, c, size;
        int array[]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4, 23,24};
    isnt that under main? sorry for the confusion =(
    also
    for(a = 0; a < elements - 1; a++)
    i see -1 used alot in arrays and loops etc, but i never understood why its used?

    also
    Code:
    sizeof(array)/sizeof(*array).
    is the obove code merely taking the whole size of the array including all whitespace and then dividing the actual elements within it to get the size i want? couldnt i just do sizeof(*array) itself?
    Last edited by mrsirpoopsalot; 09-17-2006 at 09:37 AM.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by mrsirpoopsalot
    Im a little confused about this. did i already do that?
    Code:
    int main(int argc, char *argv[]){
        char answer[2] ;
        int num, c, size;
        int array[]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4, 23,24};
    isnt that under main? sorry for the confusion =(
    Yes, you already did. I was looking at a previous post.
    also

    i see -1 used alot in arrays and loops etc, but i never understood why its used?
    An array declared like so
    Code:
    int array[12];
    has 12 elements, 0..11. array[12] isn't a valid index. So, for example, to access the last element in an array, if you know that the array has size elements, you would use array[size-1], which is the last addressable array element (if size is >0).

    also
    Code:
    sizeof(array)/sizeof(*array).
    is the obove code merely taking the whole size of the array including all whitespace and then dividing the actual elements within it to get the size i want? couldnt i just do sizeof(*array) itself?
    sizeof(*array) would give the sizeof the first element in a array; 4 for an int (on a 32-bit computer), 1 for a char, etc. sizeof(array) gives the whole size of the entire array in memory. For a 12 element in array, it would be 12*4=48. Since you're trying to get the number of elements in the array, you divide the total size by the size of one of the elements. (Since sizeof(char) is always 1, if you're calaulating the sizeof a character array you can just use 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.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    thanks that clears up alot of confusion =)
    I refined the code abit below.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void sort(int array[], int size);
    
    int main(int argc, char *argv[]){
        char answer[2] ;
        int num, i, size;
        int array[]={ 34, 12, 22, 109, 7, 6, 709, 100, 1, 4, 23,24};
        size = sizeof(array) / sizeof(*array);
      
        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{
             num = atoi(fgets(answer,sizeof(answer), stdin));
         
             switch(num){
             case 1:
             printf("%40s", "---------Sorted----------\n");
             sort(array, size);
             for(i = 0; i < size; i++)
             printf("%i\t",array[i]);
             printf("\n");
             break;
      
             case 2:
             printf("%40s", "------Unsorted-------\n");
             for(i = 0; i < size; i++)
             printf("%i\t",array[i]);
             printf("\n");
             break;
      }
             
      }while(answer[0] != 'q' );
       getchar();	
       return 0;
    }
    
    void sort(int array[], int size){
        int a, b, temp;
        for(a = 0; a < size; a++)
        for(b = 0; b < size; b++)
        if(array[a] < array[b]){
                 temp = array[a];
                 array[a] = array[b];
                 array[b] = temp;
        }
    }

  7. #22
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by mrsirpoopsalot
    also
    Quote Originally Posted by risby
    for(a = 0; a < elements - 1; a++)
    i see -1 used alot in arrays and loops etc, but i never understood why its used?
    Quote Originally Posted by dwks
    An array declared like so
    Code:
         int array[12];
    has 12 elements, 0..11. array[12] isn't a valid index. So, for example, to access the last element in an array, if you know that the array has size elements, you would use array[size-1], which is the last addressable array element
    What dwks has explained is good for why you use "a < elements" as the continuation condition of a for loop but the reason I used "a < elements -1" in the outer loop is because I was trying to get you to test all elements in the array except the last one against the next and subsequent elements. You can't check the last element against the next element because there isn't one. The code you have at the moment often checks an array element against itself!
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

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, 11:17 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM