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

NAG CL Interface Introduction
Example description
/* nag_blast_dsum (f16elc) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.3, 2022.
 */

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

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

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_blast_dsum (f16elc) 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)), double))) {
      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 (i = 0, ix = (incx > 0 ? 0 : (1 - n) * incx); i < n; i++, ix += incx)
    scanf("%lf", &x[ix]);
  scanf("%*[^\n] ");

  /* nag_blast_dsum (f16elc).
   * Sum elements of a vector of doubles */
  sumval = nag_blast_dsum(n, x, incx, &fail);

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

  /* Print the result. */
  printf("Sum of elements of x is %9.5f\n", sumval);

END:
  NAG_FREE(x);

  return exit_status;
}