qsort

using a qsort to sort struct pointers by different variables

别来无恙 提交于 2019-12-14 03:49:54
问题 I am attempting to understand the c library qsort in the context of pointers to structs. Here is the existing code that I would like to manipulate: The structure: #define MAX_NAME 20 #define NUM_MONTHS 12 typedef struct EMP { char name[MAX_NAME+1]; int monthSales[NUM_MONTHS]; int total; } Emp; The global initialization of the data and its size: Emp *data;//where all entries are kept int empSize; and I have constructed 2 arrays of Emp pointers that I would like refer to the data in different

Compare function for qsort using two fields of a structure?

♀尐吖头ヾ 提交于 2019-12-14 00:12:04
问题 Let's suppose we have a structure: struct product { char name[30]; float price; }; I want to sort it using qsort first by price, and if the prices are equal, by name. How I thought of writing the compare function: int compare(const void *a, const void *b ) { int comp = a.price - b.price; if (comp < 0 ) return 1 if (comp > 0 ) return 0; comp = strcmp(a.name, b.name); if ( comp < 0 ) return 1; else if ( comp > 0 ) return 0; } Since I have only used the usual compare function for qsort, I don't

qSort not Sorting my array

这一生的挚爱 提交于 2019-12-13 04:27:53
问题 Here is my code: #include <stdio.h> #include <stdlib.h> float comp (const void * elem1, const void * elem2) { float f = *((float*)elem1); float s = *((float*)elem2); if (f > s) return 1; if (f < s) return -1; return 0; } int main(void) { int t, n, temp, temp1, x; float input[2][50][1000]; scanf("%d", &t); for(temp=0; temp<t; temp++){ scanf("%d ", &n); for(temp1=0; temp1<n; temp1++){ scanf("%f", &input[0][temp][temp1]); } for(temp1=0; temp1<n; temp1++){ scanf("%f", &input[1][temp][temp1]); }

Using qsort() with Structs

和自甴很熟 提交于 2019-12-13 03:43:30
问题 I just started learning C and I'm still new to it. In this program I'm working with an array of structs. The structs are: typedef struct { int day; int month; int year; } Date; typedef struct { int serial_num; char full_name[15]; Date *pDate; } Person; The array is Person *people . Now I have two arrays of people and birth dates of those people (same indexes): const char* names[MAX] = { "Sasson_Sassoni", "Pooh", "James_Bond", "Elvis_is_Alive", "Shilgiya", "Cleopatra", "Sissoo_VeSimmhoo" };

How to sort a very large array in C

喜欢而已 提交于 2019-12-13 03:37:41
问题 I want to sort on the order of four million long long s in C. Normally I would just malloc() a buffer to use as an array and call qsort() but four million * 8 bytes is one huge chunk of contiguous memory. What's the easiest way to do this? I rate ease over pure speed for this. I'd prefer not to use any libraries and the result will need to run on a modest netbook under both Windows and Linux. 回答1: Just allocate a buffer and call qsort . 32MB isn't so very big these days even on a modest

Qsort based on a column in a c-string?

眉间皱痕 提交于 2019-12-13 03:29:51
问题 A class project involves sorting an array of strings, with each string containing an equal number of columns like this: Cartwright Wendy 93 Williamson Mark 81 Thompson Mark 100 Anderson John 76 Turner Dennis 56 The program accepts a command-line argument for which column to sort on, and should print out the sorted strings unmodified. I would like to use strtok to break up copies of each string into columns, and make structs for each line like this: struct line { char * line; char column_to

How to write a comparator function for qsort for a 2D array?

别来无恙 提交于 2019-12-12 12:44:11
问题 I have an n*2 sized array. I want to sort them using qsort based on their value of the 2nd column. #include<stdio.h> int cmp(const int **a, const int **b) { //return 0; //return *a[1] - *b[1] // return *a - *b; } int main() { int t; // test cases scanf("%d", &t); for(int i=0; i<t; i++) { int n; scanf("%d", &n); // size of array int **arr = (int **)malloc(n * sizeof(int *)); for(int j =0; j< n; j++) { arr[j] = (int *) malloc(2*sizeof(int)); } for(int j =0; j< 2; j++) { for(int k =0; k< n; k++)

qsort segfault in C

人盡茶涼 提交于 2019-12-12 10:17:41
问题 I'm trying to use qsort in accordance with the man page but regardless of what I try I keep getting a segfault Here's the section of code that matters int compare_dirent(const void *a, const void *b) { const struct dirent *first = (const struct dirent *) a; const struct dirent *second = (const struct dirent *) b; return first->d_ino - second->d_ino; } int process(FILE* output,const char *dirname, int flags) { struct dirent *entries = NULL; struct dirent *table[256]; int entry_num = 0; DIR

Casting function pointers

穿精又带淫゛_ 提交于 2019-12-12 09:30:55
问题 I am writing a function that receives a pointer to a comparison function and an array of MyStructs and is supposed to sort the array according to the comparison function: void myStructSort( struct MyStruct *arr, int size, int (*comp)(const struct MyStruct *, const struct MyStruct *)) { qsort(arr, size, sizeof(struct MyStruct), comp); } Unfortunately this doesn't compile because qsort expects the comparator to receive void * arguments and not const struct MyStruct * . I thought of several bad

using qsort to sort two arrays simultaneously?

落花浮王杯 提交于 2019-12-12 07:21:51
问题 I can sort a array of pointers to words so that they are ordered alphabetically, the problem is that I need to ALSO sort an integer array (the number of times that specific word is used) so that the integers are in the same place as their respective words: my code: for (i = 0; i < numWords; i++) { // prints out the words and their frequency respectively printf("%s - %d\n", dictionary[i], frequency[i]); } //sorts the dictionary so that the words are 'alphabetical' qsort(dictionary, numWords,