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

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

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

int main(void) {

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

  INIT_FAIL(fail);

  printf("nag_zeros_poly_real (c02agc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "", &n);
  if (n > 0) {
    scale = Nag_TRUE;
    if (!(a = NAG_ALLOC(n + 1, double)) || !(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;
  }

  for (i = 0; i <= n; i++)
    scanf("%lf", &a[i]);
  printf("\nDegree of polynomial = %4" NAG_IFMT "\n\n", n);

  /* nag_zeros_poly_real (c02agc).
   * Zeros of a polynomial with real coefficients
   */
  nag_zeros_poly_real(n, a, scale, z, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zeros_poly_real (c02agc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("Roots of polynomial\n\n");
  nroot = 1;
  while (nroot <= n) {
    if (z[nroot - 1].im == 0.0) {
      printf("z = %13.4e\n", z[nroot - 1].re);
      nroot += 1;
    } else {
      printf("z = %13.4e   +/- %14.4e\n", z[nroot - 1].re,
             fabs(z[nroot - 1].im));
      nroot += 2;
    }
  }
END:
  NAG_FREE(a);
  NAG_FREE(z);
  return exit_status;
}