Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define num 5
- struct library{
- char author[10];
- char name[10];
- int age;
- };
- int compareStrings(const void *a, const void *b) {
- return strcmp(*(const char **)a, *(const char **)b);
- }
- int main() {
- struct library arr[num];
- strcpy(arr[0].author, "Danik");
- strcpy(arr[1].author, "Ivan");
- strcpy(arr[2].author, "Alexander");
- strcpy(arr[3].author, "Andrew");
- strcpy(arr[4].author, "Vlad");
- strcpy(arr[0].name, "Story");
- strcpy(arr[1].name, "Aktios");
- strcpy(arr[2].name, "KPO");
- strcpy(arr[3].name, "Red hat1");
- strcpy(arr[4].name, "Matvei");
- arr[0].age = 2000;
- arr[1].age = 1900;
- arr[2].age = 2010;
- arr[3].age = 2015;
- arr[4].age = 1999;
- for (size_t i = 0; i < num; ++i) {
- printf("%s", arr[i].author);
- printf("%s", " - ");
- printf("%s", arr[i].name);
- printf("%s", " - ");
- printf("%d\n", arr[i].age);
- }
- struct library *strings[4];
- int k = 0;
- for (int i = 0; i < num; i++){
- if (arr[i].age > 2000){
- strings[k] = &arr[i];
- k++;
- }
- }
- qsort(strings, k, sizeof(strings[0]), compareStrings);
- puts ("\n");
- printf("Sorted array:\n");
- for (size_t i = 0; i < k; ++i) {
- printf("%s", strings[i]->author);
- printf("%s", " - ");
- printf("%s", strings[i]->name);
- printf("%s", " - ");
- printf("%d\n", strings[i]->age);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement