# Computer Science

Stevens Institute of Technology Assignment 1 FE 522 – C++ Programming in Finance Due Date: October 7, 2018

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 all build and .vs folders before doing so (do not delete the .vscode folders). This .zip file should be accompanied by a .pdf file containing a report (one single report for the whole assignment). I 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 (2 points). Using the desk calculator you developed in the drill of Chapter 6:

(a) Add the ability to use {} as well as () in the program, so that {(4+5)*6}/(3+4) will be a valid expression.

(b) Add a factorial operator: use a suffix ! operator to represent “factorial.” For example, the expression 7! means 7*6*5*4*3*2*1. Make ! bind tighter than * and /; that is, 7*8! means 7*(8!) rather than (7*8)!. Begin by modifying the grammar to account for a higher-level operator. To agree with the standard mathematical definition of factorial, let 0! evaluate to 1. Hint: The calculator functions deal with doubles, but factorial is defined only for ints, so just for x!, assign the x to an int and calculate the factorial of that int.

Problem 2 (2 points). Study the documentation in http://en.cppreference.com/w/cpp/ numeric/random. Choose 5 different random number distributions, generate a sample of 1000 numbers with each of them, and create a table (output to a file) with their first 2 sample moments (mean and standard deviation).

Problem 3 (2 points). Design and implement a Money class for calculations involving dollars and cents where arithmetic has to be accurate to the last cent using the 4/5 rounding rule (.5 of a cent rounds up; anything less than .5 rounds down):

1

(a) Represent a monetary amount as a number of cents in a long int, but input and output as dollars and cents, e.g., $123.45. Do not worry about amounts that don’t fit into a long int.

(b) Provide addition (Money + Money), subtraction (Money – Money), multiplication (Money * int or Money * double), and division (Money / int or Money / double) operators.

(c) Define corresponding input (>>) and output (<<) operators.

(d) Refine the class by adding a currency (given as a constructor argument). Remember operators may become invalid for two quantities of two different currencies. Define an input operator (>>) that reads monetary amounts with currency denominations, such as USD1.23 and EUR5.00, into a Money variable. Also define a corresponding output operator (<<).

Problem 4 (2 points). Design and implement a EuropeanOption class. It should have proper constructor(s) and 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 the Black & Scholes formula:

C(St, t) = N(d1)St −N(d2)Ke−r(T−t)

P (St, t) = N(−d2)Ke−r(T−t) −N(−d1)St

d1 = 1

σ √ T − t

[ ln

( St K

) +

( r +

σ2

2

) (T − t)

] d2 = d1 − σ

√ T − t ,

where

• C(St, t) is the price of a call option;

• P (St, t) is the price of a put option;

• N() is the cumulative distribution function of the standard normal distribution;

• T − t is the time to maturity (expressed in years);

• St is the spot price of the underlying asset;

• K is the strike price;

• r is the risk free rate (annual rate, expressed in terms of continuous compounding);

• σ is the volatility of returns of the underlying asset.

Hint: Don’t try to implement N() (it is difficult). Do not reinvent the wheel.

2

Problem 5. (2 points). Write a program that reads options data from a tab-separated values (TSV) file. The input file may be manually created by you, but it should contain column names, and at least 10 lines of content. When reading such a file, create a vector of EuropeanOption objects (using the class implemented in the previous problem), and then iterate over this vector calculating the value of every option. You should create an output file with the same content as the input file, plus a new column for the option prices.

3