c++ programing questions
Stevens Institute of Technology Assignment
2
FE
5
22 – C++ Programming in Finance Due Date: April 26, 2020
For every problem below, create a different project folder. You should then put all these folders
inside a .zip file with your name before submitting everything in Canvas. Remember to delete the
cmake-build-debug folders before doing so. This .zip file should be accompanied by a file
containing a report (one single report for the whole assignment). We do not provide test cases for
any of the problems, and these must be provided by you. When providing a solution to a problem,
you must be able to present test cases alongside it, otherwise it will not be accepted as a valid
solution.
If a problem requires input and/or output files, please provide the whole file content (if not large)
in the body of the report. If the file is too large to be pleasantly presented in a report (larger than a
page), please provide a sample. You should include these files in folders named “input” and “output”,
respectively, in the root folder of each project. In order for your code to work on any machine (not
only yours), use relative paths to these files in your source code:
• for input files, use: “../../input/filename.txt”
• for output files, use: “../../output/filename.txt”
Problem
1
(25 points). Design and implement an AmericanOption class. It should hold
information such as option type (call or put), spot price (of the underlying asset), strike price,
interest rate, volatility (of the underlying asset) and time to maturity. Don’t accept illegal values.
Implement a getPrice() function which gives the price of the option using a binomial tree defined
as a matrix (vector< vector
Hint: https://stackoverflow.com/questions/1766
3
186/initializing-a-two-dimensional-stdvector
Problem 2 (25 points). Design and implement an Option class as the base class for both
EuropeanOption and AmericanOption classes. It should contain all the member variables that are
common between both option types, as well as a virtual function getPrice(). Moreover, it should
contain four (non-virtual) functions to compute the numerical Greeks (getDelta() for spot price,
getRho() for interest rate, getVega() for volatility, and getTheta() for time to maturity) of a
given option. These functions should:
• Compute the price of the option by calling the virtual function getPrice();
• Apply a small bump to the required pricing factor (the size of the bump should be a parameter
of the function);
• Compute the “bumped price” of the option by calling the virtual function getPrice();
1
https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector
• Change the modified pricing factor back to its original value;
• Return the numeric approximation of the given Greek.
After all the above is done, implement an external function (to the class) which takes a Option&
as parameter and compute all four Greeks in a row. Compare the results you obtain for American
and European (call and put) options.
Problem 3 (20 points). In C, a string (often called a C string or a C-style string in C++
literature) is a zero-terminated array of characters. For example, p and q are equivalent:
char∗ p = ” asdf ” ;
char∗ q = new char [ 5 ] ;
q [ 0 ] = ’ a ’ ; q [ 1 ] = ’ s ’ ; q [ 2 ] = ’d ’ ; q [ 3 ] = ’ f ’ ; q [
4
] = 0;
In C, we cannot have member functions, we cannot overload functions, and we cannot define an
operator (such as ==) for a struct. It follows that we need a set of (nonmember) functions to
manipulate C-style strings:
(a) Write a function, char* mystrdup(char*), that copies a C-style string into memory it allocates
on the free store.
(b) Write a function, char* myfindx(char* s, char* x), that finds the first occurrence of the
C-style string x in s.
(c) Write a function, int mystrcmp(char* s1, char* s2), that compares C-style strings. Let
it return a negative number if s1 is lexicographically before s2, zero if s1 equals s2, and a
positive number if s1 is lexicographically after s2.
Do not use any standard library functions. Do not use subscripting; use the dereference
operator * instead.
Problem 4 (30 points). Implement the LinkedNode and SortedList classes as per the interfaces
(and descriptions) presented below.
Hint: https://www.geeksforgeeks.org/doubly-linked-list/
LinkedNode: This class will be used to store individual nodes of a doubly-linked list data structure.
This class should end up being quite short and simple – no significant complexity is needed nor
desired. The interface of LinkedNode
should be exactly as follows:
// The LinkedNode c l a s s w i l l be the data type for i n d i v i d u a l nodes of
// a doubly−l inked l i s t data structure .
class LinkedNode {
private :
2
https://www.geeksforgeeks.org/doubly-linked-list/
// The value contained within t h i s node .
int m_nodeValue ;
// m_prevNode points to the node that comes before t h i s node in
// the data structure . I t w i l l be n u l l p t r i f t h i s i s the f i r s t
// node .
LinkedNode∗ m_prevNode ;
// m_nextNode points to the node that comes a f t e r t h i s node in the
// data structure . I t w i l l be n u l l p t r i f t h i s i s the l a s t node .
LinkedNode∗ m_nextNode ;
public :
// The ONLY constructor for the LinkedNode c l a s s − i t takes in the
// newly created node ’ s previous pointer , value , and next pointer ,
// and assigns them . Input :
// − pointer to the node that comes before t h i s node
// − value to be stored in t h i s node
// − pointer to the node that comes a f t e r t h i s one
LinkedNode ( int value , LinkedNode∗ prev , LinkedNode∗ next ) ;
// Returns the value stored within t h i s node .
int getValue () const ;
// Returns the address of the node that comes before t h i s node .
LinkedNode∗ getPrev () const ;
// Returns the address of the node that f o l l o w s t h i s node .
LinkedNode∗ getNext () const ;
// Sets the o b je c t ’ s previous node pointer to n u l l p t r .
void setPreviousPointerToNull ( ) ;
// Sets the o b je c t ’ s next node pointer to n u l l p t r .
void setNextPointerToNull ( ) ;
// This function DOES NOT modify ” t h i s ” node . Instead , i t uses the
// pointers contained within t h i s node to change the previous and
// next nodes so that they point to t h i s node appropriately . In
// other words , i f ” t h i s ” node i s set up such that i t s prevNode
// pointer points to a node ( c a l l i t “A”) , and ” t h i s ” node ’ s
// nextNode pointer points to another node ( c a l l i t “B”) , then
// c a l l i n g setBeforeAndAfterPointers () r e s u l t s in the node we ’ re
// c a l l i n g “A” to be updated so i t s “m_nextNode” points to ” t h i s ”
// node , and the node we ’ re c a l l i n g “B” i s updated so i t s
// “m_prevNode” points to ” t h i s ” node . ” t h i s ” node i t s e l f remains
// unchanged .
void setBeforeAndAfterPointers ( ) ;
};
SortedList: This class will be used to store a doubly-linked list in an always-sorted manner, such
that the user does not specify where in the list a new value should be inserted, but rather the new
value is inserted in the correct place to maintain a sorted order. The interface to the SortedList
3
should be exactly as follows:
// The SortedList c l a s s does not store any data d i r e c t l y . Instead , i t
// contains a c o l l e c t i o n of LinkedNode objects , each of which contains
// one element .
class SortedList {
private :
// Points to the f i r s t node in a l i s t , or n u l l p t r i f l i s t i s
// empty .
LinkedNode∗ m_head ;
// Points to the l a s t node in a l i s t , or n u l l p t r i f l i s t i s empty .
LinkedNode∗ m_tail ;
public :
// Default Constructor . I t w i l l properly i n i t i a l i z e a l i s t to be
// an empty l i s t , to which values can be added .
SortedList ( ) ;
// Copy constructor . I t w i l l make a complete ( deep ) copy of the
// l i s t , such that one can be changed without a f f e c t i n g the other .
SortedList ( const SortedList& source ) ;
// Copy assignment operator . I t w i l l make a complete ( deep ) copy
// of the l i s t such that one can be changed without a f f e c t i n g the
// other .
SortedList& operator=(const SortedList& rhs ) ;
// Returns the number of nodes contained in the l i s t .
int getNumElems () const ;
// Provides the value stored in the node at the index provided in
// the 0−based ” index ” parameter . I f the index i s out of range ,
// then the function returns a pair with the f i r s t element set to
// f a l s e to indicate f a i l u r e , and the contents of the second
// element i s . Otherwise , the function returns a pair
// with the f i r s t element set to true and the second element w i l l
// contain a copy of the value at position ” index “.
pair
// Allows the user to i n s e r t a value into the l i s t . Since t h i s i s
// a sorted l i s t , there i s no need to s p e c i f y where in the l i s t to
// i n s e r t the element . I t w i l l i n s e r t i t in the appropriate
// location based on the value being inserted . I f the node value
// being inserted i s found to be ” equal to ” one or more node
// values already in the l i s t , the newly inserted node w i l l be
// placed AFTER the previously inserted nodes .
void insertValue ( int value ) ;
// Removes the front item from the l i s t . I f the l i s t was empty ,
// the function returns a pair with the f i r s t element set to f a l s e
// to indicate f a i l u r e , and the contents of the second element i s
// . I f the l i s t was not empty and the f i r s t item was
4
// s u c c e s s f u l l y removed , the function returns a pair with the
// f i r s t element set to true , and the second element w i l l be set
// to the value that was removed .
pair
// Removes the back item from the l i s t . I f the l i s t was empty , the
// function returns a pair with the f i r s t element set to f a l s e to
// indicate f a i l u r e , and the contents of the second element i s
// . I f the l i s t was not empty and the l a s t item was
// s u c c e s s f u l l y removed , the function returns a pair with the
// f i r s t element set to true , and the second element w i l l be set
// to the value that was removed .
pair
// Prints the contents of the l i s t from head to t a i l to the
// screen . Begins with a l i n e reading “Forward List Contents
// Follow :” , then prints one l i s t element per line , indented two
// spaces , then prints the l i n e “End of List Contents” to indicate
// the end of the l i s t .
void printForward () const ;
// Prints the contents of the l i s t from t a i l to head to the
// screen . Begins with a l i n e reading “Backward List Contents
// Follow :” , then prints one l i s t element per line , indented two
// spaces , then prints the l i n e “End of List Contents” to indicate
// the end of the l i s t .
void printBackward () const ;
// Clears the l i s t to an empty s t a t e without r e s u l t i n g in any
// memory leaks .
void c l e a r ( ) ;
// Destructor , which w i l l free up a l l dynamic memory associated
// with t h i s l i s t when the l i s t i s destroyed ( i . e . when a
// s t a t i c a l l y a l l o c a t e d l i s t goes out of scope or a dynamically
// a l l o c a t e d l i s t i s deleted ) .
~SortedList ( ) ;
};
Bonus (15 points). Modify the LinkedNode and SortedList classes from Problem 4 to be
template classes such that the type of the stored value is defined by the template argument. E.g.
SortedList
5