/* nag_imin_val (f16dpc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 9, 2009.
 */

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

int main(void)
{
  /* Scalars */
  Integer  exit_status, i, incx, j, k, n, xlen;
  /* Arrays */
  Integer  *x = 0;
  /* Nag Types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_imin_val (f16dpc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read the number of elements and the increment */
  scanf("%ld%ld%*[^\n] ", &n, &incx);

  xlen = MAX(1, 1 + (n - 1)*ABS(incx));

  if (n > 0)
    {
      /* Allocate memory */
      if (!(x = NAG_ALLOC(xlen, Integer)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
    }
  else
    {
      printf("Invalid n\n");
      exit_status = 1;
      goto END;
    }

  /* Input vector x */
  for (j = 0; j < xlen; j = j + incx)
    scanf("%ld", &x[j]);
  scanf("%*[^\n] ");

  /* nag_imin_val (f16dpc).
   * Get minimum value (i) and location of that value (k)
   * of Integer vector */
  nag_imin_val(n, x, incx, &k, &i, &fail);

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

  /* Print the minimum value */
  printf("Minimum element of x is %12ld\n", i);
  /* Print its location */
  printf("Index of minimum element of x is %3ld\n", k);

 END:
  NAG_FREE(x);

  return exit_status;
}