/* nag_search_double (m01nac) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /*Logical scalar and array declarations */ Nag_Boolean validate; /*Integer scalar and array declarations */ Integer exit_status = 0; Integer i, index, lenrv, m1, m2; /*Double scalar and array declarations */ double item; double *rv = 0; NagError fail; 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); fprintf(fpout, "%s\n", "nag_search_double (m01nac) Example Program Results"); fprintf(fpout, "\n"); fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld%*[^\n] ", &lenrv); if (!(rv = NAG_ALLOC(lenrv, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read in Reference Vector rv*/ for (i = 0; i < lenrv; i++) fscanf(fpin, "%lf ", &rv[i]); fscanf(fpin, "%*[^\n] "); /* Read items sought in the reference vector*/ validate = Nag_TRUE; while (fscanf(fpin, "%lf%*[^\n] ", &item) != EOF) { m1 = 0; m2 = lenrv-1; /* * nag_search_double (m01nac) * Binary search in set of double precision numbers */ index = nag_search_double(validate, rv, m1, m2, item, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_search_double (m01nac).\n%s\n", fail.message); exit_status = 1; goto END; } if (validate) { /* Print the reference vector*/ fprintf(fpout, "%s\n", "Reference Vector is:"); for (i = 0; i < lenrv; i++) fprintf(fpout, "%7.1f%s", rv[i], (i+1)%8?" ":"\n"); fprintf(fpout, "\n"); validate = Nag_FALSE; } fprintf(fpout, "\n"); fprintf(fpout, " Search for item %7.1f returned index: %4ld\n", item, index); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (rv) NAG_FREE(rv); return exit_status; }