/* 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 #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(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0; NagError fail; double *vec = 0; ptrdiff_t step; size_t i, n, *rank = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fprintf(fpout, "nag_rank_sort (m01dsc) Example Program Results\n\n"); fscanf(fpin, "%" NAG_UFMT "%" NAG_UFMT "", &n, &step); if (n >= 1) { if (!(vec = NAG_ALLOC(n, double)) || !(rank = NAG_ALLOC(n, size_t))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid n or step.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; ++i) fscanf(fpin, "%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) { fprintf(fpout, "Error from nag_rank_sort (m01dsc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, " Data Rank\n"); for (i = 0; i < n; ++i) fprintf(fpout, " %7.4f %4" NAG_UFMT "\n", vec[i], rank[i]); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (vec) NAG_FREE(vec); if (rank) NAG_FREE(rank); return exit_status; }