Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I make a floating point comparison function for use with qsort?

0
Posted

How can I make a floating point comparison function for use with qsort?

0

Q: How I can define a comparison function for sorting an array of floats using qsort function: A: Here is a simple example (called “Sort Floats”): // Sort a list of floating point values #define USE_TI89 // Compile for TI-89 #define USE_TI92PLUS // Compile for TI-92 Plus #define USE_V200 // Compile for V200 #define MIN_AMS 100 // Compile for AMS 1.00 or higher #define SAVE_SCREEN // Save/Restore LCD Contents #include // Include All Header Files // Comparison Function CALLBACK short flt_comp(const void *a, const void *b) { return fcmp (*(const float*)a, *(const float*)b); } // Main Function void _main(void) { float list[5] = {2.5, 3.18, 1.42, 4.0, 3.25}; int i; clrscr (); qsort (list, 5, sizeof (float), flt_comp); for (i = 0; i < 5; i++) printf ("%f\n", list[i]); ngetchx (); }

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123