Thursday, July 30, 2009

In turbo_c programming what are the codes to sort the problem for example in number?

For example this problem





enter a number :56


enter a number :89


enter a number :5


enter a number :1


enter a number :100





sorted value


value x[1] : 100


value x[2] : 89


value x[3] : 56


value x[4] : 5


value x[5] : 1

In turbo_c programming what are the codes to sort the problem for example in number?
Use the qsort() function in stdlib.h.





First declare a comparison function like the following:





int comparison(const void *a, const void *b)


{


return *(int *)b - *(int *)a;


}





I'm assuming x[] is an array of ints. The comparison function is used to tell the qsort() function how to decide on the order of any two values we're comparing based on whether the return value is negative, zero or positive.





Then call qsort() to sort them as follows:





qsort((void *)x, 5, sizeof(x[0]), comparison);





I've used 5 in the above line because the array has 5 numbers in your example, change this to however many numbers you have.


No comments:

Post a Comment