/* nag_rank_sort (m01dsc) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 5 revised, 1998. * Mark 7 revised, 2001. * Mark 8 revised, 2004. * */ #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif static Integer NAG_CALL compare(const Nag_Pointer a, const Nag_Pointer b) { double x = *((const double *) a); double y = *((const double *) b); return(x < y?-1:(x == y?0:1)); } #ifdef __cplusplus } #endif int main(void) { Integer exit_status = 0; NagError fail; double *vec = 0; ptrdiff_t step; size_t i, n, *rank = 0; unsigned long n_ulong, step_ulong; INIT_FAIL(fail); /* Skip heading in data file */ scanf("%*[^\n]"); printf("nag_rank_sort (m01dsc) Example Program Results\n\n"); scanf("%u%u", &n_ulong, &step_ulong); n = (size_t)n_ulong; step = (ptrdiff_t)step_ulong; if (n >= 1) { if (!(vec = NAG_ALLOC(n, double)) || !(rank = NAG_ALLOC(n, size_t))) { printf("Allocation failure\n"); exit_status = -1; goto END; } } else { printf("Invalid n or step.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; ++i) scanf("%lf", &vec[i]); /* nag_rank_sort (m01dsc). * Rank sort of set of values of arbitrary data type */ nag_rank_sort((Pointer) vec, n, step*(ptrdiff_t)(sizeof(double)), compare, Nag_Ascending, rank, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_rank_sort (m01dsc).\n%s\n", fail.message); exit_status = 1; goto END; } printf(" Data Rank\n"); for (i = 0; i < n; ++i) printf(" %7.4f %4u\n", vec[i], (unsigned long)rank[i]); END: if (vec) NAG_FREE(vec); if (rank) NAG_FREE(rank); return exit_status; }