```/* F07CE_A1W_F C++ Header Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
* Mark 26.2, 2017.
*/

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

int main(void)
{
int       exit_status = 0;
Integer   nrhs = 1, ifail = 0;

cout << "F07CE_A1W_F C++ Header Example Program Results\n\n";
// Skip heading in data file
string mystr;
getline (cin, mystr);

// Read problem size and number of right-hand-sides
Integer n;
cin >> n;
cin >> nrhs;

// Allocate arrays containing A and its factorized form, B
// and the solution X.
nagad_a1w_w_rtype *dl=0, *d=0, *du=0, *du2=0, *b=0;
double            *sol=0;
Integer           *ipiv = 0;
Integer           n1 = n-1, n2 = n-2;
if (!(dl  = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
!(ipiv   = NAG_ALLOC(n, Integer)) ||
!(sol = NAG_ALLOC(n*n, double))) {
cout << "Allocation failure\n";
exit_status = -1;
goto END;
}

// Read the tridiagonal matrix A and right hand side B, register and copy
double dd;
for (int i = 0; i<n1; i++) {
cin >> dd;
du[i].value = dd;
du[i].id = 0;
duf[i] = du[i];
}
for (int i = 0; i<n; i++) {
cin >> dd;
d[i].value = dd;
d[i].id = 0;
df[i]  = d[i];
}
for (int i = 0; i<n1; i++) {
cin >> dd;
dl[i].value = dd;
dl[i].id = 0;
dlf[i] = dl[i];
}
for (int i = 0; i<n; i++) {
for (int j = 0; j<nrhs; j++) {
cin >> dd;
int k = i + j*n;
b[k].value = dd;
b[k].id = 0;
x[k] = b[k];
}
}

// Create AD configuration data object
ifail = 0;

// Factorize the tridiagonal matrix A
ifail = 0;

// Solve the equations Ax = b for x
ifail = 0;

// Print primal solution
for (int i=0; i<n*nrhs; i++) {
}
cout << "\n\n";
NagError  fail;
INIT_FAIL(fail);
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,nrhs,sol,n,
"  Solution",0,&fail);

cout << "\n\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode    : algorithmic\n";
cout << "\n Derivatives of first solution column w.r.t. inputs:\n";

// Obtain derivatives for each output solution point

cout.setf(ios::scientific,ios::floatfield);
cout.setf(ios::right);
cout.precision(2);
for (int i=0; i<n; i++) {
cout << "\n  Solution point " << i+1 << endl;

Integer inc = 1.0;
ifail = -1;
if (ifail != 0) {
exit_status = 3;
goto END;
}

cout << "      dx/d(du) : ";
cout.width(10); cout << " ";
for (int j=0; j<n1; j++) {
cout.width(10); cout << dd;
}

cout << "\n      dx/d(d)  : ";
for (int j=0; j<n; j++) {
cout.width(10); cout << dd;
}

cout << "\n      dx/d(dl) : ";
for (int j=0; j<n1; j++) {
cout.width(10); cout << dd;
}
cout << "\n      dx/d(b)  : ";
for (int j=0; j<n; j++) {
cout.width(10); cout << dd;
}
cout << endl;
}

END:
// Remove computational data object and tape
ifail = 0;