Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- static int compare(const void *p1, const void *p2) {
- for (int i = 0; i < 3; i++) {
- if (((double *)p1)[i] > ((double *)p2)[i]) return 1;
- else if (((double *)p1)[i] < ((double *)p2)[i]) return -1;
- }
- return 0;
- }
- int main(int ac, char **av) {
- double x[][3] = {
- { 1.0, 1.0, 1.0 },
- { 2.0, 5.0, 0.0 },
- { 2.0, 4.0, 1.0 },
- { 2.0, 3.0, 2.0 },
- { 2.0, 2.0, 3.0 },
- { 2.0, 1.0, 4.0 },
- { 1.0, 5.0, 0.0 },
- { 1.0, 4.0, 1.0 },
- { 1.0, 3.0, 2.0 },
- { 1.0, 2.0, 3.0 },
- { 1.0, 1.0, 4.0 },
- { 2.0, 2.0, 2.0 }
- };
- printf("Before:\n");
- for (int i = 0; i < sizeof(x) / sizeof(double *) / 3; i++) {
- printf("%2.1f, %2.1f, %2.1f\n", x[i][0], x[i][1], x[i][2]);
- }
- qsort(x, sizeof(x) / sizeof(double *) / 3, sizeof(x[0]), compare);
- printf("After:\n");
- for (int i = 0; i < sizeof(x) / sizeof(double *) / 3; i++) {
- printf("%2.1f, %2.1f, %2.1f\n", x[i][0], x[i][1], x[i][2]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement