NAG Library Manual, Mark 27.2
```/* E04DG_T1W_F C++ Header Example Program.
*
* Copyright 2021 Numerical Algorithms Group.
* Mark 27.2, 2021.
*/

#include <dco.hpp>
#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagx02.h>
#include <stdio.h>
using namespace std;

extern "C"
{
static void NAG_CALL objfun(void *&                 ad_handle,
Integer &               mode,
const Integer &         n,
const Integer &         nstate,
Integer                 iuser[],
}

int main(void)
{
int               exit_status = 0;
cout << "E04DG_T1W_F C++ Header Example Program Results\n\n";

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

// Read problem parameters and register for differentiation
// Skip first line of data file
string mystr;
getline(cin, mystr);

Integer n;
cin >> n;

// AD routine fixed length array arguments
Integer           iuser[1], iwsav[610];
char              cwsav[1];
logical           lwsav[120];
const Charlen     name_l = 6, cwsav_l = 1;

// AD routine variable length arrays
Integer *          iwork = 0;
nagad_t1w_w_rtype *x = 0, *x_in = 0, *objgrd = 0, *work = 0;
double *           dfdx = 0;

iwork  = new Integer[n + 1];
dfdx   = new double[n];
work   = new nagad_t1w_w_rtype[13 * n];

double xr;
for (int i = 0; i < n; i++)
{
cin >> xr;
x_in[i] = xr;
}

double inc = 1.0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
x[j] = x_in[j];
}
dco::derivative(x[i]) = inc;

// Initialize sav arrays
ifail = 0;
nag::ad::e04wb("E04DGA", cwsav, 1, lwsav, 120, iwsav, 610, rwsav, 475,
ifail);

// Solve the problem
Integer iter;
objf  = 0.0;
ifail = 0;
-1, iuser, -1, ruser, lwsav, iwsav, rwsav, ifail);
dfdx[i] = dco::derivative(objf);
}

// Primal results
cout.setf(ios::scientific, ios::floatfield);
cout.precision(3);
cout << "\n Objective value = ";
cout.width(12);
cout << dco::value(objf);
cout << "\n Solution point  = ";
for (int i = 0; i < n; i++)
{
cout.width(12);
cout << dco::value(x[i]);
}

cout << "\n Estim gradient  = ";
for (int i = 0; i < n; i++)
{
cout.width(12);
cout << dco::value(objgrd[i]);
}

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

// Get derivatives of solution points
cout << "      dobjf/dx : ";
for (int i = 0; i < n; i++)
{
cout.width(12);
cout << dfdx[i];
}
cout << endl;

// Remove computational data object

delete[] iwork;
delete[] x;
delete[] dfdx;
delete[] x_in;
delete[] objgrd;
delete[] work;
return exit_status;
}

static void NAG_CALL objfun(void *&                 ad_handle,
Integer &               mode,
const Integer &         n,
const Integer &         nstate,
Integer                 iuser[],
{
// dco/c++ used here to perform AD of objfun

nagad_t1w_w_rtype x1, x2, y1, y2, expx1;

x1    = x[0];
x2    = x[1];
expx1 = exp(x1);
y1    = 2.0 * x1;
y1    = y1 + x2;
y2    = x2 + 1.0;
x1    = y1 * y1;
x2    = y2 * y2;
x1    = x1 + x2;
objf  = expx1 * x1;

if (mode == 2)
{
y2        = y1 + y2;
y2        = 2.0 * y2;
y1        = 4.0 * y1;
y1        = expx1 * y1;
objgrd[0] = y1 + objf;
objgrd[1] = expx1 * y2;
}

return;
}
```