#include <iostream>
#include <algorithm>
using namespace std;
struct book {
char name[100];
int year;
};
int compare(const void *a, const void *b) {
book *book1 = (book *) a;
book *book2 = (book *) b;
return (book2->year - book1->year);
}
int main() {
size_t dimension;
printf("Enter the dimension of books array: ");
cin >> dimension;
struct book books[dimension];
string query;
for (int i = 0; i < dimension; ++i) {
cout << "Enter name of the book" << endl;
cin >> books[i].name;
cout << "Enter year of the book" << endl;
cin >> books[i].year;
}
cout << "Entered array:" << endl;
for (int i = 0; i < dimension; ++i) {
cout << books[i].name << " - " << books[i].year << endl;
}
qsort(books, dimension, sizeof(book), compare);
cout << "Sorted array:" << endl;
for (int i = 0; i < dimension; ++i) {
cout << books[i].name << " - " << books[i].year << endl;
}
return 0;
}