8. C++ Tables


1. Introduction

1.1. Overview

Decorative artwork loosely related to the surrounding topic, without conveying any essential information.

It is possible to create spreadsheets and tables using C++, without knowing all the intricacies of the language. Although even the basics of C++ require some understanding of how to work with compilers, source files, and binaries, we will be able to avoid all of those. Our usage of C++ will be limited to the basic arithmetic and linear algebra. The course website offers some features that hide the complexities of C++ and expose only the pieces that we need for making our basic tables. To use this feature, we need to access the section Tables from the top menu.

1.2. Creating the first table

Make sure you are signed in. Click on the link Table at the top menu, and choose New.

Edit the name that you want to use for your table. Put few elements in the matrix \(A\), and then click on Save/Update.

Next time, when you click on the link Tables, and then on the link Saved, you will be able to find the work you created.

1.3. Components of the C++ tables

The C++ tables have 3 major components:

  • Input table \(A\);
  • Output table \(B\);
  • C++ code that operates on input values and produces the output table.

The formats of the input and output table are \(20\times 20\) by default.

1.4. Simplest program in 4 steps

  • Step 1.Put a value in the cells \(A[0][0]\) and \(A[0][1]\).
  • Step 2. Scroll down to the section called C++ Code. Put the following text in the box:
    B[0][0]=A[0][0]+A[0][1];
    B[1][0]=A[0][0]-A[0][1];
    Make sure that each of your instructions ends with the semicolon ; All C++ instructions must terminate with ;
  • Step 3. Save the work. Click on Save/Update
  • Step 4. Execute the code. Click on Calculate/Display. You will see the input matrix \(A\) and the output matrix \(B\).

2. Example: Absolute value of the difference between A[0][0] and A[0][1]

We will first calculate the difference

B[0][0]=A[0][0]-A[0][1];

Then, we will check whether it is negative. If it is, then we need to multiply it by \(-1\) because we need absolute value.

This is the complete code

B[0][0]=A[0][0]-A[0][1];
if(B[0][0]<0){B[0][0]=B[0][0]*(-1);}

3. Example: Sums of the first 8 elements of the row 0

The first eight cells of the row 0 are A[0][0], A[0][1], ..., A[0][7]. The sum of these cells will be placed in B[0][0].

There are three ways to do this: 1) very ugly, 2) ugly, and 3) elegant. We will do each of these.

3.1. Very ugly solution

B[0][0]=A[0][0];
B[0][0]=B[0][0]+A[0][1];
B[0][0]=B[0][0]+A[0][2];
B[0][0]=B[0][0]+A[0][3];
B[0][0]=B[0][0]+A[0][4];
B[0][0]=B[0][0]+A[0][5];
B[0][0]=B[0][0]+A[0][6];
B[0][0]=B[0][0]+A[0][7];

3.2. Ugly solution

We will now introduce a variable c that will take values 0, 1, ..., 7 and we will always use the same two instructions: B[0][0]=B[0][0]+A[0][c]; and c=c+1;.

In C++ tables every variable (except for A and B) must be introduced before it is used. The instructions that introduce variables are called declarations. The declaration of the variable c is int c;. The declaration must specify what kind of values will be stored. The variable c will store integers, hence the declaration int c;.

int c; 
c=0;
B[0][0]=0;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;

Remark: In the standard C++, every variable must be declared. In C++ tables, the variables A and B are declared by the website. So, you must not re-declare them.

3.3. Elegant solution

The subsection 3.2. has identical instructions that appear multiple times. The programming language C++ allows us to use loops. With loops, we will write the repeating instructions only once. Although we will write them only once, they will be executed multiple times. The most common loop is the while loop. We will execute the loop for as long as the number c is in the set \(\{0\), \(1\), \(2\), \(3\), \(4\), \(5\), \(6\), \(7\}\), i.e. \(c< 8\).

int c;
c=0;
while(c<8){
  B[0][0]=B[0][0]+A[0][c];
  c=c+1;
}

4. Example: The maximal and minimal difference of adjacent cells

In this problem we will take every pair of adjacent cells, calculate the absolute value of their difference, and then find which of these differences is maximal and which of them is minimal. The maximal difference will be stored in B[0][0]. The minimal difference will be stored in B[0][1].

We will declare two variables row and col of type int. We will visit each cell. We will analyze each cell A[row][col] and its two neighbors: The right neighbor A[row][col+1] and the neighbor below A[row+1][col]. We will evaluate the differences and their absolute values. Then, we'll compare these absolute values with the current maximum B[0][0] and the current minimum B[0][1]. It would be very convenient to introduce another variable diff in which we will keep the differences. The variable diff has to be declared in such a way that it can store real numbers. Real numbers in C++ are called double The declaration of diff is double diff;.

int row;
int col;
double diff;
row=0; col=0;
diff=A[0][0]-A[0][1];
if(diff<0){diff = diff *(-1);}
B[0][0]=diff; B[0][1]=diff;
while(row<20){
  while(col<20){
    if(col<19){
      diff=A[row][col]-A[row][col+1];
      if(diff<0){diff = diff *(-1);}
      if(diff>B[0][0]){B[0][0]=diff;}
      if(diff<B[0][1]){B[0][1]=diff;}
    }
    if(row<19){
      diff=A[row][col]-A[row+1][col];
      if(diff<0){diff = diff *(-1);}
      if(diff>B[0][0]){B[0][0]=diff;}
      if(diff<B[0][1]){B[0][1]=diff;}
    }
    col=col+1;
  }
  row=row+1;
}

5. Binomial Model with C++ Tables

5.1. Review of the binomial model

In the real world, the price of each stock is unpredictable. We know the current price and we use the letter \(S\) or \(S_0\) to denote this current price. All the future prices are random.

In binomial model we make some assumptions to significantly simplify the randomness.

Assumption 1. There are only \(n\) moments in the future at which the stock price will change. These prices are denoted by \(S_1\), \(S_2\), \(\dots\), \(S_n\).

Assumption 2. There exist two positive constants \(u\) and \(d\) and for each moment in time \(i\geq 1\), the price \(S_i\) must satisfy \[S_{i}\in \left\{S_{i-1}\cdot u,S_{i-1}\cdot d\right\}.\]

5.2. Example

Let us consider an example in which the input values are \(n=2\), \(S_0=S=\$200\), \(u=1.25\), \(d=0.75\). Then the price \(S_1\) satisfies \[S_1\in\left\{S\cdot u,S\cdot d\right\}=\left\{200\cdot 1.25,200\cdot 0.75\right\}=\{250,150\}.\] If the price \(S_1\) is equal to \(\$250\), then the price \(S_2\) must belong to \(\{312.5,187.5\}\). If the price \(S_1\) is equal to \(\$150\), then the price \(S_2\) must belong to \(\{187.5,112.5\}\). We can summarize these conclusions with the stock price tree

\begin{eqnarray*} \begin{array}{ccccc} \quad & \quad & \quad & \quad & \$312.5\\ & & & \nearrow & \quad\\ & & \$250& &\\ &\nearrow & &\searrow&\\ \$200& &&& \$187.5\\ &\searrow & & \nearrow&\\ & & \$150& &\\ & & & \searrow & \\ & & & & \$112.5 \end{array} \end{eqnarray*}

5.3. Stock price tree with C++ tables

5.3.1. Input variables

We first have to make a decision on where to put the values \(n\), \(S\), \(u\), and \(d\) in the input table A. Let's make have them all in the row 0. More specifically, let's have them in this order, i.e. n=A[0][0], S=A[0][1], u=A[0][2], and d=A[0][3].

We need to declare the variables n, S, u, and d in our C++ code. The variables S, u, and d are real numbers, so the following code should work

double S, u, d;
S=A[0][1]; u=A[0][2]; d=A[0][3];

The variable n is an integer, so its declaration is a bit different

int n;
n=A[0][0];

5.3.2. Data structure for stock tree

Now we need to decide on appropriate data structure for the stock tree.

At time 0, there is only one possibility for the price. That price is \(S\).

At time 1, there are two possibilities: \(S\cdot u\) and \(S\cdot d\).

At time 2, there are three possibilities: \(S\cdot u^2\), \(S\cdot ud\), and \(S\cdot d^2\).

Similarly, at time t, there are \(t+1\) possibilities.

For each time \(t\in \{0,1,\dots, n\}\), we need to store \(t+1\) elements. Therefore, the data structure stockTree has to be quite complex. For example, for time 2, the object stockTree[2] must have three elements \(S\cdot u^2\), \(S\cdot ud\), and \(S\cdot d^2\). In C++ we can use a vector to store there three elements. Hence, stockTree[2] will be a vector. Its three elements will be stockTree[2][0]\(=S\cdot u^2\), stockTree[2][1]\(=S\cdot ud\), stockTree[2][2]\(=S\cdot d^2\).

We will have a lot of vectors: stockTree[0], stockTree[1], stockTree[2], stockTree[n] in order to fit all of our data. This is a visual representation of our tree:

\begin{eqnarray*} \begin{array}{ccccc} \quad & \quad & \quad & \quad & \mbox{stockTree[2][0]}\\ & & & \nearrow & \quad\\ & & \mbox{stockTree[1][0]}& &\\ &\nearrow & &\searrow&\\ \mbox{stockTree[0][0]}& &&& \mbox{stockTree[2][1]}\\ &\searrow & & \nearrow&\\ & & \mbox{stockTree[1][1]}& &\\ & & & \searrow & \\ & & & & \mbox{stockTree[2][2]} \end{array} \end{eqnarray*}

Hence, stockTree needs to be a vector of vectors. Its declaration is

std::vector<std::vector<double> > stockTree;

We need to resize the vector stockTree so it can store all of the vectors

stockTree[0], stockTree[1], stockTree[2], stockTree[n].

The following instruction will achieve the required task.

stockTree.resize(n+1);

Now we need to make sure that each of the vectors

stockTree[0], stockTree[1], stockTree[2], stockTree[n]
is properly resized. The vector stockTree[0] must have the size \(1\), so it can store stockTree[0][0].

The vector stockTree[1] must have the size \(2\) so it can store both of the vectors stockTree[1][0] and stockTree[1][1].

The vector stockTree[2] must have the size \(3\) so it can store all three of the vectors stockTree[2][0], stockTree[2][1], and stockTree[2][2].

int i;
i=0;
while(i<=n){
  stockTree[i].resize(i+1);
  i=i+1;
}

5.3.3. Calculations of stock prices

The price stockTree[0][0] is just S.

The prices stockTree[1][0] and stockTree[1][1] can be evaluated next. They are stockTree[0][0] * u and stockTree[0][0] * d.

The prices stockTree[2][0], stockTree[2][1], and stockTree[2][2] are next. The first one, stockTree[2][0] is stockTree[1][0]*u. The remaining ones, stockTree[2][1] and stockTree[2][2] can be obtained as stockTree[0][0]*d and stockTree[1][1]*d.

stockTree[0][0]=S;
int j;
i=1;
while(i<=n){
  stockTree[i][0]=stockTree[i-1][0]*u;
  j=1;
  while(j<=i){
    stockTree[i][j]=stockTree[i-1][j-1]*d;
    j=j+1;
  }
  i=i+1;
}

5.3.4. Preparation of output

We will now transfer the calculated prices into the output table B. The prices

stockTree[0][0], stockTree[1][0], stockTree[2][0], ..., 
stockTree[n][0]
will be placed in the cells B[0][0], B[0][1], B[0][2], ..., B[0][n]. The prices
stockTree[1][1], stockTree[2][1], ..., stockTree[n][1]
will go into the cells B[1][1], B[1][2], ..., B[1][n]. Similarly, for each i, we will transfer the prices
stockTree[i][i], stockTree[i+1][i], ..., stockTree[n][i]
into the cells B[i][i], B[i][i+1], ..., B[i][n].

i=0;
while((i<=n)&&(i<20)){
  j=i;
  while((j<=n)&&(j<20)){
    B[i][j]=stockTree[j][i];
    j=j+1;
  }
  i=i+1;
}

5.3.5. Final code

double S, u, d;
S=A[0][1]; u=A[0][2]; d=A[0][3];
int n;
n=A[0][0];
std::vector<std::vector<double> > stockTree;
stockTree.resize(n+1);
int i;
i=0;
while(i<=n){
  stockTree[i].resize(i+1);
  i=i+1;
}
stockTree[0][0]=S;
int j;
i=1;
while(i<=n){
  stockTree[i][0]=stockTree[i-1][0]*u;
  j=1;
  while(j<=i){
    stockTree[i][j]=stockTree[i-1][j-1]*d;
    j=j+1;
  }
  i=i+1;
}
i=0;
while((i<=n)&&(i<20)){
  j=i;
  while((j<=n)&&(j<20)){
    B[i][j]=stockTree[j][i];
    j=j+1;
  }
  i=i+1;
}

6. Option Pricing with C++ Tables

Once we fix an underlying asset (such as a stock), we can build derived assets whose payoffs depend on the behavior of the underlying asset. The simplest among them are European call options.

6.1. Introduction to European call options

European call option is a derived security that has the following parameters:

  • Underlying asset. Usually a specific stock is the underlying asset for a European call.
  • Expiration time. A time in the future is fixed. In real world, the expiration time is quoted as a specific date in the future. In our notes, the expiration time \(T\) will be the length of time that needs to pass from the present until the expiration of the option. Thus, if we write \(T=0.25\), this would mean that the option expires in \(0.25\) years. If we write \(T=2\), we will mean that the option expires in \(2\) years.
  • Strike price. The strike price will be denoted by \(K\).
Definition of European call option. The European call option with strike \(K\) and expiration \(T\) is the contract between the buyer (who is also called the holder of the long position) and the seller (who is also called the holder of the short position) in which the buyer (i.e. the holder of the long position) is allowed but not required to buy one share of underlying asset from the seller (i.e. the holder of the short position) at the expiration time \(T\) for the price \(K\).

If the buyer decides to buy one share of the underlying asset from the seller, the seller is not allowed to refuse. Thus, if at the expiration, the price of the underlying asset is higher than the strike \(K\), the buyer will choose to exercise the option and get the underlying asset. The buyer will be paying the cheaper price \(K\), instead of the regular price \(S_T\) for the underlying asset.

Exercising the option is equivalent to the seller giving money \((S_T-K)^+\) to the buyer. The quantity \((S_T-K)^+\) is called the payoff of the option.

6.2. Option prices in binomial model

Theorem 1.

If the price of the stock is assumed to follow the binomial model with parameters \(n\), \(S\), \(u\), and \(d\), and if the simple interest rate \(r\) over one period satisfies \(u> 1+r> d\), then the price of the derived security with expiration \(n\) satisfies \begin{eqnarray*}\mbox{Price}&=& \frac1{(1+r)^n}\cdot \mathbb E_*\left[\mbox{Payoff}(S_n)\right], \end{eqnarray*} where \(\mathbb E_*\) is the expectation under the risk-neutral probability.

The risk-neutral probability \(p_*\) that in each period stock price gets multiplied by \(u\) is \begin{eqnarray*}p_*&=&\mathbb P_*\left(S_{i+1}=S_i\cdot u\right)=\frac{1+r-d}{u-d}. \end{eqnarray*} Consequenlty, the risk neutral probability \(q_*\) of the event \(\{S_{i+1}=S_i\cdot d\}\) satisfies \(q_*=1-p_*=\frac{u-1-r}{u-d}\).

6.3. Example

Let us analyze a European call option with expiration \(n=2\) and strike \(K=\$180\) whose underlying security is the stock from the example we made in subsection 5.2. The parameters in the example were \(n=2\), \(S_0=S=\$200\), \(u=1.25\), \(d=0.75\). The stock price tree was

\begin{eqnarray*} \begin{array}{ccccc} \quad & \quad & \quad & \quad & \$312.5\\ & & & \nearrow & \quad\\ & & \$250& &\\ &\nearrow & &\searrow&\\ \$200& &&& \$187.5\\ &\searrow & & \nearrow&\\ & & \$150& &\\ & & & \searrow & \\ & & & & \$112.5 \end{array} \end{eqnarray*}

We will assume that the interest rate over each period is \(r=10\%=0.1\).

Our goal is to determine the price \(C_0\) of the European call option with expiration \(n=2\) and strike \(K=\$180\).

There are \(3\) possible prices that the stock can take at the expiration \(n=2\). They are \(312.5\), \(187.5\), and \(112.5\). The possible payoffs the European call option are \begin{eqnarray*}C_{u^2}&=&\mbox{Payoff}(312.5)=(312.5-180)^+=132.5, \\C_{ud}&=&\mbox{Payoff}(187.5)=(187.5-180)^+=7.5,\quad\mbox{and}\\ C_{d^2}&=&\mbox{Payoff}(112.5)=(112.5-180)^+=0.\end{eqnarray*} We completed our first step in constructing the option price tree. The result is not a complete tree. We only have the last column.

\begin{eqnarray*} \begin{array}{ccccc} \quad & \quad & \quad & \quad & \$132.5\\ & & & \nearrow & \quad\\ & &C_u& &\\ &\nearrow & &\searrow&\\ C_0& &&& \$7.5\\ &\searrow & & \nearrow&\\ & & C_d& &\\ & & & \searrow & \\ & & & & \$0 \end{array} \end{eqnarray*}

The numbers \(C_0\), \(C_u\), and \(C_d\) are unknown at the moment. We will calculate them soon. The number \(C_u\) is the price of the option in the binomial model with one period in which the possible payoffs are \(C_{u^2}=132.5\) and \(C_{ud}=7.5\). According to the Theorem 1, the price \(C_u\) satisfies \begin{eqnarray*} C_u&=&\frac1{1+r}\mathbb E_*\left[\mbox{Payoff}\right]\\ &=&\frac1{1+r}\cdot\left(C_{u^2}\cdot p_*+C_{ud}\cdot q_*\right),\quad \mbox{where }\\ p_*&=&\frac{1+r-d}{u-d}=0.7\quad \mbox{ and }\\ q_*&=&1-p_*=0.3. \end{eqnarray*} The price \(C_u\) is \(C_u=86.363636\).

In a very similar way we obtain \begin{eqnarray*}C_d &=& \frac1{1+r}\cdot \left(C_{ud}\cdot p_*+C_{d^2}\cdot q_*\right)\\ &=& \frac1{1.1}\cdot \left(7.5\cdot 0.7+0\cdot 0.3\right)\\ &=&4.772727.\end{eqnarray*} The tree after the second step is \begin{eqnarray*} \begin{array}{ccccc} \quad & \quad & \quad & \quad & \$132.5\\ & & & \nearrow & \quad\\ & &86.363636& &\\ &\nearrow & &\searrow&\\ C_0& &&& \$7.5\\ &\searrow & & \nearrow&\\ & & 4.772727& &\\ & & & \searrow & \\ & & & & \$0 \end{array} \end{eqnarray*} Finally, \begin{eqnarray*}C_0 &=& \frac1{1+r}\cdot \left(C_{u}\cdot p_*+C_{d}\cdot q_*\right)=56.26033.\end{eqnarray*} The final tree is \begin{eqnarray*} \begin{array}{ccccc} \quad & \quad & \quad & \quad & \$132.5\\ & & & \nearrow & \quad\\ & &86.363636& &\\ &\nearrow & &\searrow&\\ 56.26033& &&& \$7.5\\ &\searrow & & \nearrow&\\ & & 4.772727& &\\ & & & \searrow & \\ & & & & \$0 \end{array} \end{eqnarray*}

6.4. Option price tree with C++ tables

In section 5 we made a program that creates the stock price tree. We will need that program now. We will extend it to calculate prices of European call options.

6.4.1. Input variables

The two additional variables that we need are the interest rate \(r\) and the strike \(K\). We will read them from A[0][4] and A[0][5].

double r,K;
r=A[0][4]; K=A[0][5];

6.4.2. Data structure for option tree

The option price tree has the same format as the stock price tree. We need a vector of \(n+1\) vectors whose lengths are \(1\), \(2\), \(\dots\), \(n+1\). Instead of manually resizing the vectors, we can just copy the data structure stockTree. Eventually, we will re-write each of its entries. This copying is done merely as a convenient way to allocate memory of correct shape.

std::vector<std::vector<double> > optionTree=stockTree;

6.4.3. Calculations of option prices

The option tree is constructed backwards. The last column is the payoff column. Its entries are the payoffs of the options at the expiration time \(n\). There are \(n+1\) elements in the payoff column because there are \(n+1\) possible prices for the stock at the expiration time \(n\). Each price of the stock determines the payoff uniquely.

i=0;
while(i<=n){
  if(stockTree[n][i]>K){
    optionTree[n][i]=stockTree[n][i]-K;
  }
  else{
    optionTree[n][i]=0;
  }
  i=i+1;
}

The remaining columns are treated differently than the last column \(n\). Let us fix \(i\) and \(j\) that satisfy\(i< n\) and \(0\leq j\leq i\).

Assume that optionTree[i+1][j] and optionTree[i+1][j+1] are already evaluated.

We can evaluate optionTree[i][j] using the formula

\[\mbox{optionTree}[i][j]=\frac1{1+r}\left(p_*\cdot \mbox{optionTree}[i+1][j]+q_*\cdot \mbox{optionTree}[i+1][j+1]\right).\]

We obtain the following code

double pStar,qStar;
pStar=(1.0+r-d)/(u-d); qStar=1.0-pStar;
i=n-1;
while(i>=0){
  j=0;
  while(j<=i){
    optionTree[i][j]=(1.0/(1.0+r))
                      * (pStar*optionTree[i+1][j]
                        +qStar*optionTree[i+1][j+1]);
    j=j+1;
  }
  i=i-1;
}

6.4.4. Preparation of output

In section 5 we made a transfer from stockTree to B. Here we will do an analogous operation. We'll just use optionTree instead of stockTree.

i=0;
while(i<=n){
  j=i;
  while(j<=n){
    B[i][j]=optionTree[j][i];
    j=j+1;
  }
  i=i+1;
}

6.4.5. Final code

double S, u, d;
S=A[0][1]; u=A[0][2]; d=A[0][3];
int n;
n=A[0][0];
std::vector<std::vector<double> > stockTree;
stockTree.resize(n+1);
int i;
i=0;
while(i<=n){
  stockTree[i].resize(i+1);
  i=i+1;
}
stockTree[0][0]=S;
int j;
i=1;
while(i<=n){
  stockTree[i][0]=stockTree[i-1][0]*u;
  j=1;
  while(j<=i){
    stockTree[i][j]=stockTree[i-1][j-1]*d;
    j=j+1;
  }
  i=i+1;
}
double r,K;
r=A[0][4]; K=A[0][5];
std::vector<std::vector<double> > optionTree=stockTree;
i=0;
while(i<=n){
  if(stockTree[n][i]>K){
    optionTree[n][i]=stockTree[n][i]-K;
  }
  else{
    optionTree[n][i]=0;
  }
  i=i+1;
}
double pStar,qStar;
pStar=(1.0+r-d)/(u-d); qStar=1.0-pStar;
i=n-1;
while(i>=0){
  j=0;
  while(j<=i){
    optionTree[i][j]=(1.0/(1.0+r))
                       * (pStar*optionTree[i+1][j]
                           + qStar*optionTree[i+1][j+1]);
    j=j+1;
  }
  i=i-1;
}
i=0;
while(i<=n){
  j=i;
  while(j<=n){
    B[i][j]=optionTree[j][i];
    j=j+1;
  }
  i=i+1;
}

7. American Put Options with C++ Tables

7.1. American put options

The buyer of the European put option has the right, but not the obligation, to sell one share of the underlying asset at the expiration time for the strike price \(K\). The buyer of the American put has the right to exercise the option at any time prior to and including the expiration \(T\).

The price of the American put is calculated by constructing the put price tree. The tree is constructed backwards. The last, payoff, column is constructed first. Then we move backwards. Every time we evaluate two prices. The first price to calculate is the price that can be gained by exercising the put. The second price, the holding value is the value of the option if the exercising were not allowed. This holding price is obtained using the formula \begin{eqnarray*}\mbox{Price}&=& \frac1{1+r}\cdot \mathbb E_*\left[\mbox{Payoff}(S_n)\right]. \end{eqnarray*}

7.2. Code that constructs the option price tree

The code is obtained by modifying the program made in section 6. When we apply the changes described in the previous paragraph, we obtain the program given below.

double S, u, d;
S=A[0][1]; u=A[0][2]; d=A[0][3];
int n;
n=A[0][0];
std::vector<std::vector<double> > stockTree;
stockTree.resize(n+1);
int i;
i=0;
while(i<=n){
  stockTree[i].resize(i+1);
  i=i+1;
}
stockTree[0][0]=S;
int j;
i=1;
while(i<=n){
  stockTree[i][0]=stockTree[i-1][0]*u;
  j=1;
  while(j<=i){
    stockTree[i][j]=stockTree[i-1][j-1]*d;
    j=j+1;
  }
  i=i+1;
}
double r,K;
r=A[0][4]; K=A[0][5];
std::vector<std::vector<double> > optionTree=stockTree;
i=0;
while(i<=n){
  if(stockTree[n][i]<K){
    optionTree[n][i]=K-stockTree[n][i];
  }
  else{
    optionTree[n][i]=0;
  }
  i=i+1;
}
double pStar,qStar;
pStar=(1.0+r-d)/(u-d); qStar=1.0-pStar;
i=n-1;
double exerciseValue, holdingValue;
while(i>=0){
  j=0;
  while(j<=i){
    exerciseValue=0;
    if(stockTree[i][j]<K){
      exerciseValue=K-stockTree[i][j];
    }
    holdingValue=(1.0/(1.0+r))
                  * (pStar*optionTree[i+1][j]
                     + qStar*optionTree[i+1][j+1]);
    if(exerciseValue>holdingValue){
      optionTree[i][j]=exerciseValue;
    }
    else{
      optionTree[i][j]=holdingValue;
    }
    j=j+1;
  }
  i=i-1;
}
i=0;
while(i<=n){
  j=i;
  while(j<=n){
    B[i][j]=optionTree[j][i];
    j=j+1;
  }
  i=i+1;
}