/* nag_robust_trimmed_1var (g07ddc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 *
 */

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

#define NMAX 1000
int main(void)
{

  /* Local variables */
  Integer exit_status = 0, i, k, n;
  NagError fail;
  double alpha, propn, *sx = 0, tmean, tvar, wmean, wvar, *x = 0;

  INIT_FAIL(fail);

  printf("nag_robust_trimmed_1var (g07ddc) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT " ", &n);
  if (n >= 2) {
    if (!(x = NAG_ALLOC(NMAX, double)) || !(sx = NAG_ALLOC(NMAX, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    printf("Invalid n.\n");
    exit_status = 1;
    return exit_status;
  }
  for (i = 1; i <= n; ++i)
    scanf("%lf ", &x[i - 1]);
  scanf("%lf ", &alpha);

  /* nag_robust_trimmed_1var (g07ddc).
   * Trimmed and winsorized mean of a sample with estimates of
   * the variances of the two means
   */
  nag_robust_trimmed_1var(n, x, alpha, &tmean, &wmean, &tvar, &wvar, &k, sx,
                          &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_robust_trimmed_1var (g07ddc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  propn = (double) k / n;
  propn = 100.0 - propn * 200.0;
  printf("Statistics from middle %6.2f%% of data\n\n", propn);
  printf("               Trimmed-mean = %11.4f\n", tmean);
  printf("   Variance of Trimmed-mean = %11.4f\n\n", tvar);
  printf("            Winsorized-mean = %11.4f\n", wmean);
  printf("Variance of Winsorized-mean = %11.4f\n", wvar);
END:
  NAG_FREE(x);
  NAG_FREE(sx);
  return exit_status;
}