Question: Write the comparison function of integers for the qsort function. int int_compare(const void* p, const void* q); In general, the comparison function for the qsort

Write the comparison function of integers for the qsort function.

int int_compare(const void* p, const void* q);

In general, the comparison function for the qsort function must return an integer that is:

Negative if *p is less than *q

Zero if *p is equal to *q

Positive if *p is greater than *q

Fill in the code in sort_ints.c (given below):

#include

#include

int int_compare(const void* p, const void* q);

int main()

{

int n, i;

int *a;

printf("Enter the length of the array: ");

scanf("%d", &n);

a = malloc(n*sizeof(int));

for(i = 0; i < n; i++)

{

printf("Enter a number: ");

scanf("%d", &a[i]);

}

qsort(a, n, sizeof(int), int_compare);

printf("In sorted order: ");

for(i = 0; i < n; i++)

printf("%d\t", a[i]);

printf(" ");

return 0;

}

int int_compare(const void* p, const void* q){

//code to be filled in

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!