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