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

NAG CL Interface Introduction
Example description
/* nag_specfun_opt_imp_vol (s30acc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */
#include <nag.h>
#include <stdio.h>
#include <string.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, mode, n;
  /* Arrays */
  Integer *ivalid = 0;
  double *sigma = 0, *t = 0, *r = 0, *s0 = 0, *k = 0, *p = 0;
  /* Character declarations */
  char put[8 + 1];
  /* Nag Types */
  Nag_CallPut putnum;
  NagError fail;

  INIT_FAIL(fail);

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

  /* Read put */
  scanf("%8s%*[^\n] ", put);
  /*
   * nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  putnum = (Nag_CallPut)nag_enum_name_to_value(put);

  printf("nag_specfun_opt_imp_vol (s30acc) Example Program Results\n");
  printf("\n");
  printf("     sigma   ivalid\n");
  printf("\n");
  /* Read in the values of n and mode*/
  scanf("%" NAG_IFMT "", &n);
  scanf("%" NAG_IFMT "", &mode);
  scanf("%*[^\n]");

  /* Allocate memory */
  if (!(sigma = NAG_ALLOC(n, double)) || !(s0 = NAG_ALLOC(n, double)) ||
      !(r = NAG_ALLOC(n, double)) || !(k = NAG_ALLOC(n, double)) ||
      !(t = NAG_ALLOC(n, double)) || !(p = NAG_ALLOC(n, double)) ||
      !(ivalid = NAG_ALLOC(n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < n; i++)
    scanf("%lf", &p[i]);
  scanf("%*[^\n]");

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

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

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

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

  /* nag_specfun_opt_imp_vol (s30acc).
   * Black-Scholes-Merton implied volatility
   */
  nag_specfun_opt_imp_vol(putnum, n, p, k, s0, t, r, sigma, mode, ivalid,
                          &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_specfun_opt_imp_vol (s30acc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  for (i = 0; i < n; i++)
    printf(" %11.3e %4" NAG_IFMT "\n", sigma[i], ivalid[i]);

END:
  NAG_FREE(sigma);
  NAG_FREE(p);
  NAG_FREE(t);
  NAG_FREE(r);
  NAG_FREE(s0);
  NAG_FREE(k);
  NAG_FREE(ivalid);

  return exit_status;
}