Thread: Filter Sort

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

    Filter Sort

    Right I have a 2D array and stored in one of the elements... is the values 1 / 0........ What I want to be able to do is the print the array if the value is 1 ////////////// and have another sort to print the array if the value is ==0 ......... Any ideas at the moment I am running 3 other qsorts in the same program. along the lings of this
    switch(sort) {

    case 'a':

    case 'A':

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

    }

    the information for this required sort stated above is stored in

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

    thanks for your time

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    For such a filtering, I wouldn't sort the array. If you need it otherwise, sort it, but for filtering go through the array in a loop and query each element with a simple if. No sort will ever match the speed of one 'if' per record.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

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

    didn't really answer my question

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


    How can I do that when the data is stored in here

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

    thanks

    greg

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    18
    PLZ help !!!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    sorting rearrages the records into some order (say all females before all males)

    filtering is just selection, like so
    Code:
    printf("\n\rOnly females, print d"); 
    for ( i = 0 ; i < n ; i++ ) {
      if ( a[i].sex == 0 ) {
        // print the record
      }
    }
    And please don't bump your thread

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

    just tired that and

    #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->wage - a2->wage);

    }

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

    SLIST *a1=(SLIST *)v1;

    SLIST *a2=(SLIST *)v2;



    return (a1->age - 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);

    }
    if(sort='d')

    {
    for ( i = 0 ; i < n ; i++ ) {
    if ( a[i].sex == 0 ) {

    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);
    }


    }
    if(sort='e')

    {
    for ( i = 0 ; i < n ; i++ ) {
    if ( a[i].sex == 1 ) {

    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("\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);
    }
    }
    }
    }
    }


    }






    HMMM doesn't work Salem ...... have I done something wrong ???

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > have I done something wrong ???
    Continual failure to read
    http://www.cprogramming.com/cboard/m...bbcode#buttons
    and figuring out that
    &#91;code]
    // paste code here
    &#91;/code]
    is a good idea

    > if(sort='d')
    should be
    if(sort=='d')

    Ditto for 'e'

    Which compiler are you using?
    Most modern compilers will warn you of this, if you turn on enough warning enabling flags.

    For instance, with gcc - you would use the -Wall flag
    gcc -Wall prog.c

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    18
    I am using visual studio 6 ......... didn't solve anything unfortunately V_V

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    /* You have this */
    if(sort=='d') { 
        for(i = 0 ; i < n ; i++) { 
            if(a[i].sex == 0) { 
                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 ); 
                }
            }
        }
    }
    
    /* you should try this */
    if(sort=='d') { 
        printf( "\t Survey Data:\n" ); 
        printf( "\t~~~~~~~~~~~~~~~~~~~\n" ); 
        for(i = 0 ; i < n ; i++) { 
            if(a[i].sex == 0) { 
                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 ); 
            }
        }
    }

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    18
    thanks your a life saver !!!!!!!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM