/* nag_dload (f16fbc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 8, 2005.
 */

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

int main(void)
{

  /* Scalars */
  double   alpha;
  Integer  exit_status, i, incx, n, xlen;

  /* Arrays */
  double   *x = 0;

  /* Nag Types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_dload (f16fbc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read length of vector and increment. */
  scanf("%ld%ld%*[^\n] ", &n, &incx);

  /* Read scalar parameter */
  scanf("%lf%*[^\n] ", &alpha);

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

  /* nag_dload (f16fbc).
   * Broadcast a real scalar to a real vector.
   *
   */
  nag_dload(n, alpha, x, incx, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dload.\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print x. */
  printf("Loaded vector x:\n\n");
  for (i = 0; i < xlen; i = i + incx)
    printf(" x[%1ld] = %5.2f\n", i, x[i]);
 END:
  NAG_FREE(x);

  return exit_status;
}