Advertisement
Vladislav8653

Untitled

Nov 14th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define num 5
  5. struct library{
  6. char author[10];
  7. char name[10];
  8. int age;
  9. };
  10.  
  11. int compareStrings(const void *a, const void *b) {
  12. return strcmp(*(const char **)a, *(const char **)b);
  13. }
  14.  
  15. int main() {
  16.  
  17. struct library arr[num];
  18. strcpy(arr[0].author, "Danik");
  19. strcpy(arr[1].author, "Ivan");
  20. strcpy(arr[2].author, "Alexander");
  21. strcpy(arr[3].author, "Andrew");
  22. strcpy(arr[4].author, "Vlad");
  23. strcpy(arr[0].name, "Story");
  24. strcpy(arr[1].name, "Aktios");
  25. strcpy(arr[2].name, "KPO");
  26. strcpy(arr[3].name, "Red hat1");
  27. strcpy(arr[4].name, "Matvei");
  28. arr[0].age = 2000;
  29. arr[1].age = 1900;
  30. arr[2].age = 2010;
  31. arr[3].age = 2015;
  32. arr[4].age = 1999;
  33. for (size_t i = 0; i < num; ++i) {
  34. printf("%s", arr[i].author);
  35. printf("%s", " - ");
  36. printf("%s", arr[i].name);
  37. printf("%s", " - ");
  38. printf("%d\n", arr[i].age);
  39. }
  40. struct library *strings[4];
  41. int k = 0;
  42. for (int i = 0; i < num; i++){
  43. if (arr[i].age > 2000){
  44. strings[k] = &arr[i];
  45. k++;
  46. }
  47. }
  48. qsort(strings, k, sizeof(strings[0]), compareStrings);
  49. puts ("\n");
  50. printf("Sorted array:\n");
  51. for (size_t i = 0; i < k; ++i) {
  52. printf("%s", strings[i]->author);
  53. printf("%s", " - ");
  54. printf("%s", strings[i]->name);
  55. printf("%s", " - ");
  56. printf("%d\n", strings[i]->age);
  57. }
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement