NAG Library Manual, Mark 30
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_blast_imax_val (f16dnc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

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

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

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_blast_imax_val (f16dnc) Example Program Results\n\n");

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

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

  /* Read the vector x and store forwards or backwards
   * as determined by incx. */
  for (j = 0, jx = (incx > 0 ? 0 : (1 - n) * incx); j < n; j++, jx += incx)
    scanf("%" NAG_IFMT "", &x[jx]);
  scanf("%*[^\n] ");

  /* nag_blast_imax_val (f16dnc).
   * Get maximum value (i) and location of that value (k)
   * of Integer vector */
  nag_blast_imax_val(n, x, incx, &k, &i, &fail);

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

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

END:
  NAG_FREE(x);

  return exit_status;
}