Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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++):

image text in transcribed

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

You are given three files: 1. compare Elements h 2. compare Functions.h 3. sort Me.c You are to write the functions declared in compareFunctions.h 1. int compare id ascending const void const void 2. int compare id descending const void const void 3. int compare name ascending const void const void 4. int compare name descending const void const void 5. nt compare money ascending const void const void 6. int compare money descending (const void const void The program sortMe.c uses the gsort function to sort an array of structures (the structure definition is in compareElements.h) n six different ways Write the six comparison functions. Put each function in a separate file and write a makefile that separately compiles them and then links them to the sortMe.c program to produce the executable sort Me

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions

Question

1. Where do these biases come from?

Answered: 1 week ago

Question

7. What decisions would you make as the city manager?

Answered: 1 week ago