```/* E01BG_A1W_F C++ Header Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
* Mark 27, 2019.
*/

#include <nag.h>
#include <stdio.h>
#include <math.h>
#include <nag_stdlib.h>
#include <string>
#include <iostream>
using namespace std;

int main(void)
{
// Scalars
int               exit_status = 0;

cout << "E01BG_A1W_F C++ Header Example Program Results\n\n";

// Skip first line of data file
string mystr;
getline (cin, mystr);
// Read number of data points
Integer n;
cin >> n;

// Allocate arrays for data and interpolant
nagad_a1w_w_rtype *x = 0, *f = 0, *d = 0;
if (!(x = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
printf("Allocation failure\n");
exit_status = -2;
}
if (exit_status==0) {

// Create AD configuration data object
Integer ifail = 0;

// Read data and register variables
for (int i=0; i<n; i++) {
double xr, fr;
cin >> xr >> fr;
x[i].value = xr;
x[i].id = 0;
f[i].value = fr;
f[i].id = 0;
}

ifail = 0;

// Evaluate interpolant and derivatives at a mid-point
const Integer     m = 1;
double            xint;
px[0].value = xint;
px[0].id = 0;
ifail = 0;

cout << "\n Value of interpolant at x = " << xint;
cout.precision(5);
cout << " is: " << nagad_a1w_get_value(pf[0]) << endl;

// Setup evaluation of derivatives via adjoints.
double inc = 1.0;

ifail = 0;

cout << "\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode    : algorithmic\n";

// Get derivatives
cout << "\n Derivatives of fitted value w.r.t. data points:\n\n";
cout << "    i     d/dx         d/df\n";
cout.setf(ios::scientific,ios::floatfield);
cout.precision(4);
for (int j=0; j < n; j++) {
cout.width(5); cout << j+1;
cout.width(12); cout << dx;
cout.width(12); cout << dy << endl;
}

// Remove computational data object and tape