qsort

qsort does not work for double array

爱⌒轻易说出口 提交于 2021-02-20 03:43:22
问题 I try to sort an array of double value using qsort, but it doesn't seems to work. Wonder what has gone wrong here?? #include <stdio.h> #include <stdlib.h> static double compare (const void * a, const void * b) { if (*(double*)a > *(double*)b) return 1; else if (*(double*)a < *(double*)b) return -1; else return 0; } int main() { int idx; double* sum_least_square_err; sum_least_square_err = (double*) malloc (2500*2500*sizeof(double)); sum_least_square_err[0] = 0.642; sum_least_square_err[1] = 0

qsort does not work for double array

南笙酒味 提交于 2021-02-20 03:42:39
问题 I try to sort an array of double value using qsort, but it doesn't seems to work. Wonder what has gone wrong here?? #include <stdio.h> #include <stdlib.h> static double compare (const void * a, const void * b) { if (*(double*)a > *(double*)b) return 1; else if (*(double*)a < *(double*)b) return -1; else return 0; } int main() { int idx; double* sum_least_square_err; sum_least_square_err = (double*) malloc (2500*2500*sizeof(double)); sum_least_square_err[0] = 0.642; sum_least_square_err[1] = 0

How to qsort a dirent in C

元气小坏坏 提交于 2021-02-11 15:26:54
问题 Brand new to C and finding it confusing. What I really want to know about, is taking two separate pieces of code and getting them to work together. Here's some code that simply lists the contents of the current directory: #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main (void) { DIR *dp; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) puts (ep->d_name); (void) closedir (dp); } else perror ("Couldn't open the directory"); return 0; }

How to qsort a dirent in C

我只是一个虾纸丫 提交于 2021-02-11 15:25:56
问题 Brand new to C and finding it confusing. What I really want to know about, is taking two separate pieces of code and getting them to work together. Here's some code that simply lists the contents of the current directory: #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main (void) { DIR *dp; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) puts (ep->d_name); (void) closedir (dp); } else perror ("Couldn't open the directory"); return 0; }

Sorting an array of strings in C

末鹿安然 提交于 2021-02-10 16:43:25
问题 I have an assignment I've been working on for a few hours now, and I can't seem to get it quite right. The assignment is to take a random number of names (from stdin), sort them, and then output them in alphabetical order. I can't find any sites online that handle this kind of sorting specifically, and have had no luck trying to implement qsort() into my code. #include <stdio.h> #include <string.h> #include <stdlib.h> int stringcmp(const void *a, const void *b) { const char **ia = (const char

Is there a way to sort structs by multiple variables in C?

﹥>﹥吖頭↗ 提交于 2021-02-10 05:44:06
问题 I have to write a function that sorts structs in an array. the struct is: #define MAX_USERNAME_LENGTH 16 typedef struct{ char username[MAX_USERNAME_LENGTH]; unsigned int rides; unsigned int rank; } driver; The program load the data from a .txt file and fills an array driver driver_list[256] I have to sort driver_list by ranks AND number of rides. So if my file contains //user rides rank frank209 3 6 john76 7 6 harry99 2 2 bob77 5 2 Output must show: john76 7 6 frank209 3 6 bob77 5 2 harry99 2

What means of this code in C qsort?

会有一股神秘感。 提交于 2021-01-28 23:56:03
问题 void qsort (void *a, size_t n, size_t es, int (*compare)(const void *, const void *) where a is a start of array address, n is sizeof array, es is sizeof array element. I read the source code of qsort in C that I can't understand. the code is as follows. #define SWAPINT(a,es) swaptype = ((char*)a- (char*)0 % sizeof(long) || \ es % sizeof(long) ? 2: es == sizeof(long)? 0 : 1 I interpret this macro by, if(((char*)a- (char*)0)% sizeof(long))==1 || es % sizeof(long)==1) swaptype = 2; else if(es==

What means of this code in C qsort?

萝らか妹 提交于 2021-01-28 23:31:02
问题 void qsort (void *a, size_t n, size_t es, int (*compare)(const void *, const void *) where a is a start of array address, n is sizeof array, es is sizeof array element. I read the source code of qsort in C that I can't understand. the code is as follows. #define SWAPINT(a,es) swaptype = ((char*)a- (char*)0 % sizeof(long) || \ es % sizeof(long) ? 2: es == sizeof(long)? 0 : 1 I interpret this macro by, if(((char*)a- (char*)0)% sizeof(long))==1 || es % sizeof(long)==1) swaptype = 2; else if(es==

Can the qsort comparison function always return a non-zero value?

牧云@^-^@ 提交于 2021-01-07 01:41:33
问题 An ascending sort callback function for qsort and bsearch on an array of int could look like this: int ascending(const void *o1, const void *o2) { int a = *(const int *)o1; int b = *(const int *)o2; return a < b ? -1 : 1; } Yet this function seems to violate the constraint on the compar function as specified in the C Standard: 7.22.5.2 The qsort function Synopsis #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); Description The

Can the qsort comparison function always return a non-zero value?

我与影子孤独终老i 提交于 2021-01-07 01:40:48
问题 An ascending sort callback function for qsort and bsearch on an array of int could look like this: int ascending(const void *o1, const void *o2) { int a = *(const int *)o1; int b = *(const int *)o2; return a < b ? -1 : 1; } Yet this function seems to violate the constraint on the compar function as specified in the C Standard: 7.22.5.2 The qsort function Synopsis #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); Description The