4CMP留学生讲解、讲解C++编程语言、辅导C++设计、讲解Canvas

- 首页 >> C/C++编程


Problem Sheet 1, 4CMP Spring Term 2018/19


PROBLEM SHEET 1

4CMP, SPRING TERM, PART 2: PROGRAMMING IN C++

Lecturer: Dr Fabian Spill (f.spill@bham.ac.uk)

Due Date: Saturday 2

nd February 2019 at 11.59 pm

Weighting: This problem sheet counts 15% to your final mark for the spring part of 4CMP, but only

the best two out of problem sheets 1-3 count.

INSTRUCTIONS FOR THE SUBMISSION:

Submit your code by the deadline on Canvas. Each problem on this sheet should be implemented in

a single file named as follows: main_X.cpp. Here, X is the number of the problem. For example,

main_P1.cpp. Additionally, problem 2 requires some explanations that should be submitted as a pdf

file.

This file should compile without errors or warnings on Visual Studio 2015 as installed on the clusters

in the learning centre, and the executable should run without problems.

The source code should also be well formatted. This means that opening and closing brackets should

be aligned in a readable way, and we will discuss such styles briefly in the lectures. Also, it is crucial

that you comment your code well. For this first problem sheet, this means that every single nontrivial

statement you write should be explained with a comment. Moreover, at the beginning of each

file, give some brief description of what the code in this file is doing. You can follow the style I use

(which contains author, version, file, description etc, but you can also create your own style of

comments. The critical bit is that it contains the information required to follow what is going on in

the code). The programs you write should run in a self-explanatory way. If you are asked that the

program provides some input, it should first have some output on the command line telling the user

what kind of input is required. For instance, if you are supposed to read in the price of a share, do

the following:

double price;

std::cout << “ Please enter the price of share: “ << std::endl;

std::cin >> price;

Then, note that obviously group work or copying solutions from other students or sources

constitutes plagiarism and is not allowed.

Marking: The most important criteria for marking is the correctness of the code. It needs to compile

and run correctly, and do what you were asked to do. However, style, efficiency, readability and

formatting are also part of the evaluation criteria. In particular, if the code is so unreadable that one

cannot evaluate if it is working correctly, then you risk to loose many marks for that.

PROBLEM 1: MEAN, STANDARD DEVIATION AND CORRELATION

Write a function that calculates the sample mean and standard deviation for two vectors that are

input for these functions, and the correlation between the two vectors. If the length of the two

vectors is not the same, an error message should be given. The function should then “return” the

means, standard deviations and correlations. You should implement this “return” by using

references as arguments of the function as we discussed in the lectures.

The main program should declare two double-valued std::vector x,y that are populated with the

values

x = {1.2, 4.3, 2.3, 3.3}

y = {2.2, 3.1, 4.3, 2.1}.

Call the function that performs the calculations of means, standard deviation and correlation, and

then write the outputs (means, standard deviation and correlation) to the command line.

PROBLEM 2: A DOUBLE SUM

Write a function that has a single integer m as an input and returns the following sum as an integer:

In main, ask the user to input m and call the function that calculates the above sum. Then, compare

your result with the analytic result that is given by

(1 + m). Have the program check if the results of the direct calculation and the formula above

agree, and write the result of both the check and the actual numerical results on the command line.

If you encounter unexpected problems during the comparison, notice that the combined arithmetic

operations with multiplications and divisions are evaluate are evaluated left to right. This means for

three integers a, b, c, the product a * b * c is evaluated as (a * b ) * c. Likewise, division. So, can you

think of what could potentially go wrong when you rearrange the factors in a product?

Finally, check what the largest integer m is before your calculation exceeds the maximal integer,

which is 2

31 1. Is that maximum different for the direct calculation of the sum and for the analytic

formula 1

(2 + m)(1 + m )? Which m is the smallest for which the analytic

calculation matches the calculation through the sum. Can you explain your answer? Submit your

answer and explanations as a pdf file on canvas.

PROBLEM 3: BLACK-SCHOLES

First, write a function that returns the value of the cumulative distribution function of a Gaussian

distribution. For this purpose, you can use the complementary error function erfc, recalling that the

Gaussian CDF is obtained by CDF(x) = erfc(-x /sqrt(2)).

Both the function erfc and sqrt are in the library cmath. That means if at the beginning of your

source code, you write: #include <cmath>, then you can simply use erfc and sqrt in your code.

Then, write a function that calculates the price of a European Call option through the Black-Scholes

formula. The input for this function should be the current share price, the current time, the expiry

time, the strike price, the volatility, the interest rate and the dividend yield. Motivate your choice of

appropriate data types in your code comments. This function should call the Gaussian CDF function

that you created before. Next, write a function that calculates the value of a European Put option.

The function main should have the following functionality: It should declare the following nondimensional

parameters for testing purposes: share price: 50, strike: 50, Expiration: 1, Interest rates:

0.1, Volatitility 0.5, Divident: 0.2. The functions for put and call prices should be called with these

parameters, and the returned put and call prices written on the command line.

Problem Sheet 1, 4CMP Spring Term 2018/19

PROBLEM 4: SINE FUNCTION

Write a function that calculates the Taylor approximation

for the sine function to

arbitrary N. In main, call this function for inputs x and N that are obtained from the command line,

and then write the result of the Taylor approximation to the command line. Also write the output of

the function: “std::sin” which you can find in <cmath> on the command line, and calculate the error

between the Taylor expansion and the value obtained from the cmath function. When you have

solved this exercise, think also of the efficiency of your calculations. Each arithmetic operation costs

computational time. Thus, have you tried to keep your number of arithmetic operations low?



站长地图