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

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

int main(void)
{

  Nag_Boolean scale;
  Complex *a = 0, *z = 0;
  Integer exit_status = 0, i, n;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_zeros_complex_poly (c02afc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "", &n);
  if (n > 0) {
    if (!(a = NAG_ALLOC(n + 1, Complex)) || !(z = NAG_ALLOC(n, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    printf("Invalid n.\n");
    exit_status = 1;
    return exit_status;
  }
  scale = Nag_TRUE;
  for (i = 0; i <= n; i++)
    scanf("%lf%lf", &a[i].re, &a[i].im);

  /* nag_zeros_complex_poly (c02afc).
   * Zeros of a polynomial with complex coefficients
   */
  nag_zeros_complex_poly(n, a, scale, z, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zeros_complex_poly (c02afc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\nDegree of polynomial = %4" NAG_IFMT "\n\n", n);
  printf("Roots of polynomial\n\n");
  for (i = 0; i < n; ++i)
    printf("z = %13.4e   %+14.4e\n", z[i].re, z[i].im);
END:
  NAG_FREE(a);
  NAG_FREE(z);
  return exit_status;
}