/* nag_durbin_watson_stat (g02fcc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  /* Scalars */
  double d, pdl, pdu;
  Integer exit_status, i, p, n;
  NagError fail;

  /* Arrays */
  double *res = 0;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_durbin_watson_stat (g02fcc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &p);
  n = 10;

  /* Allocate memory */
  if (!(res = NAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  for (i = 1; i <= n; ++i)
    scanf("%lf", &res[i - 1]);
  scanf("%*[^\n] ");

  /* nag_durbin_watson_stat (g02fcc).
   * Computes Durbin-Watson test statistic
   */
  nag_durbin_watson_stat(n, p, res, &d, &pdl, &pdu, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_durbin_watson_stat (g02fcc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  printf(" Durbin-Watson statistic %10.4f\n\n", d);
  printf(" Lower and upper bound %10.4f%10.4f\n", pdl, pdu);
END:
  NAG_FREE(res);
  return exit_status;
}