include iostream include algorithm using namespace std struct student

 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
47
48
#include <iostream>
#include <algorithm>
using namespace std;
struct student {
char name[100];
int group;
};
int compare(const void *a, const void *b) {
student *student1 = (student *) a;
student *student2 = (student *) b;
return (student1->group - student2->group);
}
int main() {
size_t dimension = 3;
cout << "Enter 3 students for sorting: " << endl;
struct student students[dimension];
string query;
for (int i = 0; i < dimension; ++i) {
cout << "Enter name of the student[" << i << "]" << endl;
cin >> students[i].name;
cout << "Enter group of the student" << i << "]" << endl;
cin >> students[i].group;
}
cout << "Entered array:" << endl;
for (int i = 0; i < dimension; ++i) {
cout << students[i].name << " - " << students[i].group << endl;
}
qsort(students, dimension, sizeof(student), compare);
cout << "Sorted array by group:" << endl;
for (int i = 0; i < dimension; ++i) {
cout << students[i].name << " - " << students[i].group << endl;
}
return 0;
}