Throw away members in qsort

This is a discussion on Throw away members in qsort within the C Programming forums, part of the General Programming Boards category; Code: /* I need help How can I move away from qsort all members that are not have enought points ...

  1. #1
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178

    Throw away members in qsort

    Code:
    /* I need help 
    How can I move away from qsort all members that are not have enought points */
    /* I wanna do some filer function (base on qsort) that throw away all field elements that doesent satisfy criterion (what is incoming argument of function filer)*/
    /* In function does_have_enought_points all members with let's say udenr 100 points. drop out*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 1024
    #define MAXS 128
    
    struct student{
           char name[MAX];
           char lastname[MAX];
           int points;
    };
    
    typedef struct student STUDENT;
    
    int does_have_enought_points( const void *a )
    {
    }
    
    void filter( void *base, int *num, int width, int(*fn_criterion)(const void*))
    {
    }
    
    
    int compare_points(const void *a, const void *b)
    {
        return ( ((STUDENT*)a)->points - ((STUDENT*)b)->points);
    }
    
    
    void load( STUDENT a[MAXS], int n)
    {
         int i;
         for(i = 0; i < n; i++)
         {
               printf("Insert name: "); scanf("%s", &a[i].name);
               printf("Insert lastname: "); scanf("%s", &a[i].lastname);
               printf("Insert points: "); scanf("%d", &a[i].points);
         }
    }
    
    void write_out( STUDENT a[MAXS], int n)
    {
         int i;
         for( i = 0; i < n; i++ )
         {
              printf("%s ", a[i].name);
              printf("%s ", a[i].lastname);
              printf("%d ", a[i].points);
              printf("\n");
         }
    }
    
    int main()
    {
        STUDENT a[MAXS];
        int n = 0;
    
        printf("How many students do you wanna enter?\n");
        scanf("%d", &n );
        load( a, n );
    
        filter( a, &n, sizeof(STUDENT), does_have_enought_points);
        write_out( a, n );
    
        qsort( a, n, sizeof(STUDENT), compare_points);
        write_out( a, n );
    
        return 0;
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    You sort the array in decreasing score.
    Search from the start of the array for the minimum score you'll accept.
    Throw the remainder of the array away.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Can you please explain me what am I calling here. I forgot to mention that I dont understand it all.
    I got this for homework.

    Code:
    int does_have_enought_points( const void *a )
    {
    ...
    }
    
    void filter( void *base, int *num, int width, int(*fn_criterion)(const void*))
    {
    ...
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    I fail to see what those functions have to do with qsort.
    Sure, it's qsort-like, seeing as you're passing pointers to functions.

    Can you show how you're trying to use those functions.

    Rough Idea
    Code:
    int does_have_enought_points( const void *a )
    {
      const struct student *p = a;
      return p->points >= 100;
    }
    
    void filter( void *base, int *num, int width, int(*fn_criterion)(const void*))
    {
      char *p = base;  // so ptr math works
      char *q = base;
      int i, j = 0;
      for ( i = 0 ; i < *n ; i++ ) {
        if ( fn_criterion( p ) ) {
          // copy a passing record to the next passing slot
          memmove( q, p, width );
          q += width;
          j++;
        }
        p += width;
      }
      *n = j; // number of passing answers
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Thank you.
    What I have done is copy paste your code and it worked only one thing is that i rename two times *n in *num. And it worked.

    Now I see that qsort function under is completly unneed.
    What I have done is that I insert few people and all people who has under some sum of points. Are of. They are not on the list.

    Thank you Salem!
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 11:09 AM
  2. Why does C++ need throw()
    By meili100 in forum C++ Programming
    Replies: 19
    Last Post: 11-10-2007, 11:34 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Parent/child object design
    By lyx in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2003, 08:46 AM

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