There is a structure

struct internet_prov { char name[20]; unsigned int speed; float tariff; }; 

You need to write a function that performs an action on an array (size 10) of pointers to structures of this type. The function should display the K (user-defined) structures with the most expensive fare. That is, first we need to sort this array by the tariff field. Tell me, please, how to do this?

  • one
    use the standard qsort function and redefine the comparator in it. - pavel
  • Have you tried writing the function code yourself? What are the difficulties? - 0xdb
  • @pavel I planned to do so, but I don’t know how to indicate what needs to be sorted by the field of tariff - Pasha
  • @ 0xdb Yes, how to sort the array exactly by the required field of the structure - Pasha
  • @ Pasha Komporator write. You have one answer already. Try and publish the result. - 0xdb

2 answers 2

You can use the standard C function qsort , declared in the header of <stdlib.h> .

Below is a sample program that shows how you can sort an array of pointers to a structure.

 #include <stdlib.h> #include <stdio.h> #include <string.h> int cmp(const void *a, const void *b) { const struct internet_prov *left = *(const struct internet_prov **)a; const struct internet_prov *right = *(const struct internet_prov **)b; return (right->tariff < left->tariff) - (left->tariff < right->tariff); } #define N 3 int main( void ) { struct internet_prov * prov[N]; for (size_t i = 0; i < N; i++) { char name[] = "A"; name[0] += i; prov[i] = (internet_prov *)malloc(sizeof(internet_prov)); strcpy(prov[i]->name, name); prov[i]->speed = rand() % 100; prov[i]->tariff = rand() % 100 / 10.0f; } for (size_t i = 0; i < N; i++) { printf("%s %u %f\n", prov[i]->name, prov[i]->speed, prov[i]->tariff); } printf("\n"); qsort(prov, N, sizeof(struct internet_prov *), cmp); for (size_t i = 0; i < N; i++) { printf("%s %u %f\n", prov[i]->name, prov[i]->speed, prov[i]->tariff); } for (size_t i = 0; i < N; i++) { free(prov[i]); } } 

The output of the program to the console may look as follows.

 A 41 6.700000 B 34 0.000000 C 69 2.400000 B 34 0.000000 C 69 2.400000 A 41 6.700000 
  • An interesting return . And why not as usual (l < r ? -1 : l == r ? 0 : 1) ? - avp
  • 2
    @avp In programming, the same problem can be solved in different ways. :) - Vlad from Moscow
  • @VladfromMoscow and what will the cmp function look like if I work with an array of structures and sort them in descending order? - Pasha
  • @ Pasha The function body will look like const struct internet_prov left = * (const struct internet_prov *) a; const struct internet_prov right = * (const struct internet_prov *) b; return (left-> tariff <right-> tariff) - (right-> tariff <left-> tariff); - Vlad from Moscow
  • @ Pasha I wrote wrong. Will be return (left.tariff <right.tariff) - (right.tariff <left.tariff); That is, the appeal to the members of the object through a point, since it is no longer a pointer. - Vlad from Moscow

Use qsort with an appropriate comparison function, roughly

 int comp(const void * p1, const void * p2) { struct internet_prov * i1 = (struct internet_prov*)p1; struct internet_prov * i2 = (struct internet_prov*)p2; // Сортируем по тарифу if (i1->tariff > i2->tariff) return 1; if (i1->tariff < i2->tariff) return -1; // Потом сортируем по скорости if (i1->speed > i2->speed) return 1; if (i1->speed < i2->speed) return -1; // и по названию return strcmp(i1->name,i2->name); } 
  • 2
    i1->speed > i2->tariff something is not right) - pavel
  • @pavel copy-paste fails :( - Harry