Thread: Sorting (typec)

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    18

    Talking Sorting (typec)

    Hi please may u have a read and see why my third sort on age is not working correctly ........ id and wage work fine but for some reaason age is malfunctioning thanks greatly !

    #include<stdio.h>

    #include <stdlib.h>

    #include <string.h>



    typedef struct surv_list SLIST;

    struct surv_list {

    int id;

    int wage;

    int age;

    int sex;

    };



    int sort_id(const void *v1, const void *v2) {

    SLIST *a1=(SLIST *)v1;

    SLIST *a2=(SLIST *)v2;



    return (a1->id - a2->id);

    }

    int sort_wage(const void *v1, const void *v2) {

    SLIST *a1=(SLIST *)v1;

    SLIST *a2=(SLIST *)v2;



    return (a1->id - a2->wage);

    }

    int sort_age(const void *v1, const void *v2) {

    SLIST *a1=(SLIST *)v1;

    SLIST *a2=(SLIST *)v2;



    return (a1->id - a2->age);

    }

    int main()

    {

    int t;

    char sort;

    int /* PersonID,Wage,Sex,Age, */ n, i;

    SLIST a[10], *aptr=a;

    printf("please enter the amount of people you will use \n");

    scanf("%d",&n);

    printf("\n=%d\n\n", n);

    for (i=0; i<n; i++)

    {

    printf("please enter the Person ID (person %d)\n", i);

    scanf("%d",&a[i].id);

    printf("please enter the Wage (person %d)\n", i);

    scanf("%d",&a[i].wage);

    printf("please enter the Age(person %d)\n", i);

    scanf("%d",&a[i].age);

    printf("please enter the Sex use 1 for male and 0 for female(person %d)\n", i);

    scanf("%d",&a[i].sex);

    }



    printf("\t Survey Data:\n");

    printf("\t~~~~~~~~~~~~~~~~~~~\n");

    for (i=0; i<n; i++)

    {

    printf("Person %d\t", i);

    printf("ID: %d\t", a[i].id);

    printf("Wage: %d\t", a[i].wage);

    printf("Sex: %d\t", a[i].sex);

    printf("Age: %d\t\n", a[i].age);
    }



    t=1;

    while (t==1)

    {

    printf("\n\rHow do you want the data to be sorted ?");

    printf("\n\rAscending by Person ID,type a");

    printf("\n\rAscending by Wage, type b");

    printf("\n\rAscending by Age, type c");

    printf("\n\rOnly females, print d");

    printf("\n\rOnly males, print e\n\r");

    scanf("\n%c",&sort);
    switch(sort) {

    case 'a':

    case 'A':

    qsort(a, n, sizeof(SLIST), sort_id);

    }
    switch(sort) {

    case 'b':

    case 'B':

    qsort(a, n, sizeof(SLIST), sort_wage);

    }
    switch(sort) {

    case 'c':

    case 'C':

    qsort(a, n, sizeof(SLIST), sort_age);

    }









    printf("\t Survey Data:\n");

    printf("\t~~~~~~~~~~~~~~~~~~~\n");

    for (i=0; i<n; i++)

    {

    printf("Person %d\t", i);

    printf("ID: %d\t", a[i].id);

    printf("Wage: %d\t", a[i].wage);

    printf("Sex: %d\t", a[i].sex);

    printf("Age: %d\t\n", a[i].age);
    }



    printf("Would you like to sort the data again Yes = 1 No = 0 \n");

    scanf("%d",&t);
    }


    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:

    int sort_wage(const void *v1, const void *v2) {
    SLIST *a1=(SLIST *)v1;
    SLIST *a2=(SLIST *)v2;
    return (a1->id - a2->wage); //Should be a1->wage
    }

    int sort_age(const void *v1, const void *v2) {
    SLIST *a1=(SLIST *)v1;
    SLIST *a2=(SLIST *)v2;
    return (a1->id - a2->age); //Should be a1->age
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    18

    Talking

    thanks your a life saver ...... plus I am a dumbass when it comes to typing lol !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM