1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
float binomialTree(float **tree,
float S0,
float u,
float d,
int nbStep)
{
tree[0][0] = S0;
// arbre binomiale
for (int j=1; j <= nbStep ; j++)
{tree[0][j]=tree[0][j-1] * u ;}
for (int i=1; i <= nbStep ; i++)
{
for (int j=1; j <= nbStep ; j++)
if (j >= i)
{tree[i][j]=tree[i-1][j-1] * d;}
else
{tree[i][j]= 0;}
}
}
int main()
{
float S0 = 100, u = 1.1, d = 0.85;
int nbStep=15;
float **tree;
// initialisation mémoire du tableau
tree = new float* [ nbStep + 1 ];
for (int i=0; i < nbStep + 1; i++)
{tree[i] = new float[ nbStep + 1 ];}
binomialTree(tree,S0, u, d, nbStep);
// print
for (int i=0; i<=nbStep; ++i)
{
for (int j=0; j<=nbStep; ++j)
{printf("%-00005.1f ", tree[i][j]);}
printf("\n");
}
// vider mémoire
delete[] tree;
} |
Partager