```/* nag_sort_realvec_vec_search (m01ndc) Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
*
* Mark 27.0, 2019.
*/

#include <stdio.h>
#include <nag.h>

static int ex1(double *rv, Integer n, Integer m1, Integer m2,
double *item, Integer m);
static int ex2(double *rv, Integer n, Integer m1, Integer m2,
double *item, Integer m);

int main(void){

/* Scalars */
Integer exit_status = 0;
Integer exit_status_ex1 = 0;
Integer exit_status_ex2 = 0;
Integer i, n, m1, m2, m;

/* Arrays */
double *item = 0, *rv = 0;

printf("nag_sort_realvec_vec_search (m01ndc) Example Program Results\n");
printf("\n");

/* Read dimensions of arrays from data file */
scanf("%*[^\n] ");
scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &n, &m1, &m2);
scanf("%" NAG_IFMT "%*[^\n]", &m);

/* Allocate arrays accordingly */
if (!(rv = NAG_ALLOC((n), double))    ||
!(item = NAG_ALLOC((m), double))
)
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}

/* Read array values from data file */
for(i=0;i<n;i++){
scanf("%lf", &rv[i]);
}
scanf("%*[^\n]");

printf("Vector to be searched is:\n");
for(i=0;i<n;i++)
printf("%7.1f%s", rv[i], (i+1)%8 ? " " : "\n");

for(i=0;i<m;i++){
scanf("%lf", &item[i]);
}
scanf("%*[^\n]");

exit_status_ex1 = ex1(rv, n, m1, m2, item, m);
exit_status_ex2 = ex2(rv, n, m1, m2, item, m);

END:
NAG_FREE(rv);
NAG_FREE(item);

return (exit_status == 0 && exit_status_ex1 == 0 && exit_status_ex2 == 0)
? 0 : 1;
}

/* Example 1: Calling nag_sort_realvec_vec_search with mode=2 */
static int ex1(double *rv, Integer n, Integer m1, Integer m2, double *item,
Integer m){

/* Logicals */
Nag_Boolean validate;

/* Scalars */
Integer exit_status = 0;
Integer i, mode, lk;
double h = 0;

/* Arrays */
Integer *idx = 0, *k = 0;

/* Nag Types */
NagError fail;

INIT_FAIL(fail);

/* First call nag_sort_realvec_vec_search with mode=0 and to calculate */
/* the necessary values for h and lk */
mode = 0;
lk = 0;

if (!(idx = NAG_ALLOC((m), Integer))
)
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}

/* Validate input parameters the first time */
/* nag_sort_realvec_vec_search is called */
validate = Nag_TRUE;

/* nag_sort_realvec_vec_search (m01ndc) */
/* Search in a set of real numbers using an O(1) method */
nag_sort_realvec_vec_search(validate, mode, rv, n, m1, m2, item, m, idx,
&h, k, &lk, &fail);

if (fail.code != NE_NOERROR) {
printf("Error from nag_sort_realvec_vec_search (m01ndc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}

/* Allocate k using the value of lk returned in the previous call */
if (!(k = NAG_ALLOC((lk), Integer))) {
printf("Allocation failure\n");
exit_status = -2;
goto END;
}

/* There is no need to validate input parameters again */
validate = Nag_FALSE;

/* Call nag_sort_realvec_vec_search with mode=1 to populate k */
mode = 1;

/* nag_sort_realvec_vec_search (m01ndc), see above */
nag_sort_realvec_vec_search(validate, mode, rv, n, m1, m2, item, m, idx,
&h, k, &lk, &fail);

if (fail.code != NE_NOERROR) {
printf("Error from nag_sort_realvec_vec_search (m01ndc).\n%s\n",
fail.message);
exit_status = 2;
goto END;
}

/* Finally set mode=2 and call nag_sort_realvec_vec_search */
/* to search rv for the items */
mode = 2;

/* nag_sort_realvec_vec_search (m01ndc), see above */
nag_sort_realvec_vec_search(validate, mode, rv, n, m1, m2, item, m, idx,
&h, k, &lk, &fail);

if (fail.code != NE_NOERROR) {
printf("Error from nag_sort_realvec_vec_search (m01ndc).\n%s\n",
fail.message);
exit_status = 3;
goto END;
}

printf("\n\nExample 1\n\n");
for(i=0; i<m; i++) {
printf("  Search for item  %7.1f returned index: %4" NAG_IFMT "\n",
item[i], idx[i]);
}

END:
NAG_FREE(k);
NAG_FREE(idx);

return exit_status;
}

/* Example 2: Calling nag_sort_realvec_vec_search with mode=3 */
static int ex2(double *rv, Integer n, Integer m1, Integer m2, double *item,
Integer m){

/* Logicals */
Nag_Boolean validate;

/* Scalars */
Integer exit_status = 0;
Integer i, mode, lk;
double h = 0;

/* Arrays */
Integer *idx = 0, *k = 0;

/* Nag Types */
NagError fail;

INIT_FAIL(fail);

if (!(idx = NAG_ALLOC((m), Integer))){
printf("Allocation failure\n");
exit_status = -1;
goto END;
}

/* Validate input parameters */
validate = Nag_TRUE;

/* Mode 3 does not use h or k, so k does not need to be allocated */
mode = 3;
h = 0.0;
lk = 0;

/* Make a single call to nag_sort_realvec_vec_search to search rv for */
/* the items without using h or k */

/* nag_sort_realvec_vec_search (m01ndc) */
/* Search in a set of real numbers */
nag_sort_realvec_vec_search(validate, mode, rv, n, m1, m2, item, m, idx,
&h, k, &lk, &fail);

if (fail.code != NE_NOERROR) {
printf("Error from nag_sort_realvec_vec_search (m01ndc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}

printf("\n\nExample 2\n\n");
for(i=0; i<m; i++) {
printf("  Search for item  %7.1f returned index: %4" NAG_IFMT "\n",
item[i], idx[i]);
}

END:
NAG_FREE(idx);

return exit_status;
}
```