include iostream using namespace std struct book string name int year

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
struct book {
string name;
int year;
};
book *sort(book *a, size_t n) {
book *array = a;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (array[j].year > array[j + 1].year) {
book tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
return array;
}
int main() {
size_t n;
cout << "Enter count of books:" << endl;
cin >> n;
book books[n];
for (int i = 0; i < n; i++) {
cout << "Name: " << endl;
cin >> books[i].name;
cout << "year: " << endl;
cin >> books[i].year;
}
cout << "Entered array:" << endl;
for (int i = 0; i < 4; i++) {
cout << "Name: " << books[i].name << " year: " << books[i].year << endl;
}
cout << endl;
book *books2 = sort(books, n);
cout << "Sorted array:" << endl;
for (int i = 0; i < n; i++) {
cout << "Name: " << books2[i].name << " year: " << books2[i].year << endl;
}
return 0;
}