include iostream include algorithm using namespace std union point int

 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <algorithm>
using namespace std;
union point {
int decimal;
double _float;
void init() {
cin >> _float;
if ((int) _float == _float) {
decimal = (int) _float;
}
}
double get() {
if (_float != NULL) return _float;
return decimal;
}
};
struct points {
point x;
point y;
point z;
};
int comparator(const void *a, const void *b) {
points *first = (points *) a;
points *second = (points *) b;
return (first->z.get() - second->z.get());
}
int main() {
size_t n;
printf("Enter the size of points array: ");
cin >> n;
struct points _points[n];
for (int i = 0; i < n; ++i) {
cout << "Enter value of x for point[" << i << "]: ";
_points[i].x.init();
cout << "Enter value of y for point[" << i << "]: ";
_points[i].y.init();
cout << "Enter value of z for point[" << i << "]: ";
_points[i].z.init();
}
qsort(_points, n, sizeof(points), comparator);
cout << "Array sorted by z:" << endl;
for (int i = 0; i < n; ++i) {
cout << " x = " << _points[i].x.get();
cout << " y = " << _points[i].y.get();
cout << " z = " << _points[i].z.get() << endl;
}
}