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

NAG CL Interface Introduction
Example description
/* nag_stat_test_shapiro_wilk (g01ddc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 *
 */

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

int main(void) {

  /* Local variables */
  Nag_Boolean calwts;
  Integer exit_status = 0, i, j, n;
  NagError fail;
  double *a = 0, pw, w, *x = 0;

  INIT_FAIL(fail);

  printf("nag_stat_test_shapiro_wilk (g01ddc) Example Program Results\n");

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

  calwts = Nag_TRUE;
  scanf("%" NAG_IFMT " ", &n);
  if (n >= 3) {
    if (!(a = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  } else {
    printf("Invalid n.\n");
    exit_status = 1;
    return exit_status;
  }
  for (j = 1; j <= 2; ++j) {
    for (i = 1; i <= n; ++i)
      scanf("%lf ", &x[i - 1]);

    /* nag_sort_realvec_sort (m01cac).
     * Quicksort of set of values of data type double
     */
    nag_sort_realvec_sort(x, (size_t)n, Nag_Ascending, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_sort_realvec_sort (m01cac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    /* nag_stat_test_shapiro_wilk (g01ddc).
     * Shapiro and Wilk's W test for Normality
     */
    nag_stat_test_shapiro_wilk(n, x, calwts, a, &w, &pw, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_stat_test_shapiro_wilk (g01ddc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\n For sample number %2" NAG_IFMT ", value of W statistic = "
           "%7.4f\n",
           j, w);
    printf("                       Significance level is %8.4f\n", pw);
    calwts = Nag_FALSE;
  }
END:
  NAG_FREE(a);
  NAG_FREE(x);
  return exit_status;
}