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