Can someone please help me write the functions C code, I have to use qsort. The
ID: 3806545 • Letter: C
Question
Can someone please help me write the functions C code, I have to use qsort.
The program sortMe.c uses the qsort function to sort an array of structures struct element {int id_number; char last_name[10]; float salary;}; You are to write the functions int compare_id_ascending (const void *, const void *); int compare_id_descending (const void *, const void *); int compare_name_ascending (const void *, const void *); int compare_name_descending (const void *, const void *); int compare_money_ascending (const void *, const void *); int compare_money_descending (const void *, const void *);Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
struct element{
int id_number;
char last_name[10];
float salary;
};
int compare_id_ascending(const void *e1, const void *e2){
struct element *a = (struct element *)e1;
struct element *b = (struct element *)e2;
return (a->id_number - b->id_number);
}
int compare_id_descending(const void *e1, const void *e2){
struct element *a = (struct element *)e1;
struct element *b = (struct element *)e2;
return (b->id_number - a->id_number);
}
int compare_name_ascending(const void *e1, const void *e2){
struct element *a = (struct element *)e1;
struct element *b = (struct element *)e2;
return strcmp(a->last_name, b->last_name);
}
int compare_name_descending(const void *e1, const void *e2){
truct element *a = (struct element *)e1;
struct element *b = (struct element *)e2;
return strcmp(b->last_name, a->last_name);
}
int compare_money_ascending(const void *e1, const void *e2){
struct element *a = (struct element *)e1;
struct element *b = (struct element *)e2;
return (a->salary - b->salary);
}
int compare_money_descending(const void *e1, const void *e2){
struct element *a = (struct element *)e1;
struct element *b = (struct element *)e2;
return (b->salary - a->salary);
}