#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Need help Array sorting


    assignment is due soon, need to sort the employees by name first last and by id number

    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define SIZE 5
    #define SUCCESS 1
    #define FAILURE 0

    struct Employee {
    char first[11];
    char last[11];
    int id;
    };

    int employeeSort(struct Employee *people, int size, char sortOn);

    int main() {
    int i, success;
    struct Employee emp[] = { {"Dalton", "McGuinty", 5291},
    {"Brad", "Whal", 3449},
    {"Greg", "Selinger", 2848},
    {"Alison", "Redford", 7868},
    {"Christy", "Clark", 8691}
    };

    // Print out the unsorted array.
    printf("The unsorted array of employees is:\n");
    for(i = 0; i < SIZE; i++)
    printf("%10s %-10s id# = %d\n", emp[i].first, emp[i].last, emp[i].id);

    // Test sorting by first name.
    printf("\nThe array of employees sorted by first name is:\n");
    success = employeeSort(emp, SIZE, 'f');
    if(success) for(i = 0; i < SIZE; i++)
    printf("%10s %-10s id# = %d\n", emp[i].first, emp[i].last, emp[i].id);
    else printf("\tan incorrect argument to the sortOn parameter.\n");

    // Test sorting by last name.
    printf("\nThe array of employees sorted by last name is:\n");
    success = employeeSort(emp, SIZE, 'l');
    if(success) for(i = 0; i < SIZE; i++)
    printf("%10s %-10s id# = %d\n", emp[i].first, emp[i].last, emp[i].id);
    else printf("\tan incorrect argument to the sortOn parameter.\n");

    // Test sorting by id number.
    printf("\nThe array of employees sorted by id is:\n");
    success = employeeSort(emp, SIZE, 'i');
    if(success) for(i = 0; i < SIZE; i++)
    printf("%10s %-10s id# = %d\n", emp[i].first, emp[i].last, emp[i].id);
    else printf("\tan incorrect argument to the sortOn parameter.\n");

    // Test sorting by an invalid sortOn parameter.
    printf("\nThe array of employees sorted by something other is:\n");
    success = employeeSort(emp, SIZE, 'x');
    if(success) for(i = 0; i < SIZE; i++)
    printf("%10s %-10s id# = %d\n", emp[i].first, emp[i].last, emp[i].id);
    else printf("\tan incorrect argument to the sortOn parameter.\n");

    // Exit gracefully
    printf("\n\nAll done. Press any key to exit... ");
    _getch();
    return 0;
    }

    /******************************************************************************
    if sortOn is 'f' then sort by first name and return a SUCCESS;
    else if sortOn is 'l' then sort by last name and return a SUCCESS;
    else if sortOn is 'i' then sort by id number and return a SUCCESS;
    else if sortOn is anything else, return a FAILURE;
    *******************************************************************************/
    int employeeSort(struct Employee *people, int size, char sortOn) {
    int i, j;
    char (*temp);

    for (i = 1; i < size; i++) {
    temp = people[i];
    for (j = i-1; j>=0 && strcmp(temp, people[j])<0; j--) people[j+1] = people[j];
    people[j+1] = temp;
    }
    }



    What i have so far!! Help!!1
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Use qsort.
    Code:
    /* you could write fewer functions by setting an "extract data function" */
    /* and you could make arrays of functions, thereby condense main statements */
    /* into a neat loop instead of all that terrible duplicate code */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define DIM(A) (sizeof(A)/(sizeof(*(A))))
    
    struct Employee {
      char first[11];
      char last[11];
      int id;
    };
    
    static int cmp_first_name(const void*a,const void*b) {
      struct Employee*c = (struct Employee*)a, *d = (struct Employee*)b;
      return strcmp(c->first,d->first);
    }
    
    static int cmp_last_name(const void*a,const void*b) {
      struct Employee*c = (struct Employee*)a, *d = (struct Employee*)b;
      return strcmp(c->last,d->last);
    }
    
    static int cmp_id_number(const void*a,const void*b) {
      struct Employee*c = (struct Employee*)a, *d = (struct Employee*)b;
      return c->id - d->id;
    }
    
    static int cmp_random(const void*a,const void*b) {
      return random() % 3 - 1;	/* slightly skewed random */
    }
    
    static void display(struct Employee*e,int n) {
      int i;
      for(i = 0; i < n; i++)
        printf("%10s %-10s id# = %d\n", e[i].first, e[i].last, e[i].id);
    }
      
    int main() {
      struct Employee emp[] = {
        {"Dalton", "McGuinty", 5291},
        {"Brad", "Whal", 3449},
        {"Greg", "Selinger", 2848},
        {"Alison", "Redford", 7868},
        {"Christy", "Clark", 8691}
      };
      puts("The unsorted array of employees is:");
      display(emp,DIM(emp));
    
      qsort(emp,DIM(emp),sizeof(*emp),cmp_first_name);
      puts("\nThe array of employees sorted by first name is:");
      display(emp,DIM(emp));
    
      qsort(emp,DIM(emp),sizeof(*emp),cmp_last_name);
      puts("\nThe array of employees sorted by last name is:");
      display(emp,DIM(emp));
    
      qsort(emp,DIM(emp),sizeof(*emp),cmp_id_number);
      puts("\nThe array of employees sorted by id is:");
      display(emp,DIM(emp));
    
      qsort(emp,DIM(emp),sizeof(*emp),cmp_random);
      puts("\nThe array of employees sorted randomly is:");
      display(emp,DIM(emp));
    
      printf("\n\nAll done. Press any enter to exit... ");
      getchar();
      return 0;
    }
    Last edited by b49P23TIvg; December 3rd, 2012 at 07:28 PM. Reason: Use casts to remove warning messages.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Ya it has to look the same as the instructors, that would change it, just need to add a couple more loops at the end but im new at this and dont know what to declare for temp
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Study my program and implement the comments at top. You might not pass your class but you'll know more than your instructor knows.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    ok thanks lol

    Ps: passing the class this is just for better marks
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Study my program and implement the comments at top. You might not pass your class but you'll know more than your instructor knows.
    Ive studied your code, It makes way more sense then anything i have learned so far. really helpful!!! it still wont compile for me, what am i missing?
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    1) I modified the program to make it compile without warning messages. Maybe this will help you?

    2) if not, please show your compiler messages.
    I used gcc on a linux system.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    > return c->id - d->id;
    Whilst this seems like a nice trick, it does produce the wrong answer if the subtraction results in numeric underflow.
    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

IMN logo majestic logo threadwatch logo seochat tools logo