Question
follow the instruction, write a program in C language (no c++): sortMe.c #include #include #include compareFunctions.h #include compareElements.h int main() { int i = 0;
follow the instruction, write a program in C language (no c++):
sortMe.c
#include
#include
#include "compareFunctions.h"
#include "compareElements.h"
int main()
{
int i = 0;
int size;
int (*compare_ptr)(const void *a, const void *b);
struct element array[] = { { 10, "Brown", 10000.0 },
{ 1, "White", 20000.0 },
{ 20, "Black", 30000.0 },
{ 5, "Pink", 50000.0 },
{ 3, "Orange", 25000.0 },
{ 2, "Gray", 200000.0 } };
size = 6;
printf ("Original Array: ");
for ( i=0; i printf ( "%02d. %10s %10.2f ", array[i].id_number, array[i].last_name, array[i].salary ); } compare_ptr = &compare_id_ascending; qsort ( array, size, sizeof(struct element), compare_ptr); printf ("Sorted by ID Number (ascending): "); for ( i=0; i printf ( "%02d. %10s %10.2f ", array[i].id_number, array[i].last_name, array[i].salary ); } compare_ptr = &compare_id_descending; qsort ( array, size, sizeof(struct element), compare_ptr); printf ("Sorted by ID Number (descending): "); for ( i=0; i printf ( "%02d. %10s %10.2f ", array[i].id_number, array[i].last_name, array[i].salary ); } compare_ptr = &compare_name_ascending; qsort ( array, size, sizeof(struct element), compare_ptr); printf ("Sorted by Last Name (ascending): "); for ( i=0; i printf ( "%02d. %10s %10.2f ", array[i].id_number, array[i].last_name, array[i].salary ); } compare_ptr = &compare_name_descending; qsort ( array, size, sizeof(struct element), compare_ptr); printf ("Sorted by Last Name (descending): "); for ( i=0; i printf ( "%02d. %10s %10.2f ", array[i].id_number, array[i].last_name, array[i].salary ); } compare_ptr = &compare_money_ascending; qsort ( array, size, sizeof(struct element), compare_ptr); printf ("Sorted by Salary (ascending): "); for ( i=0; i printf ( "%02d. %10s %10.2f ", array[i].id_number, array[i].last_name, array[i].salary ); } compare_ptr = &compare_money_descending; qsort ( array, size, sizeof(struct element), compare_ptr); printf ("Sorted by Salary (descending): "); for ( i=0; i printf ( "%02d. %10s %10.2f ", array[i].id_number, array[i].last_name, array[i].salary ); } return 0; } compareElements.h /* * Structure */ struct element { int id_number; char last_name[10]; float salary; }; compareFunctions.h /* * Functions to do comparison for qsort */ 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 * );
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started