Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

MTH6150

Numerical Computing in C and C++

Exercise Sheet 8

1. Run the code on slides 6 and 7 to verify the different behaviour when passing by value or by reference.

Also run the code on slide 14 to check that it does what it says, i.e.  swap the values of the input variables. Add code to output a and b after the swap function to confirm this. Change the arguments in the swap function to passing by value

swap(int m, int n)

and see what happens.

2. Initialize two vectors (to contain type double) with values {2.0, 1.0, 0.0} and {−1.0, 2.0, 0.0}, respectively. Calculate the inner product of these vectors using the function on slide 19.

3. Write a function output to output a vector<double> to the console (the black window that cout writes to), with a space between each element.  The function should take a const reference argument.

Write a function of the same name that outputs a vector<int>, also with a const reference argument.

Check that these work as expected by creating each kind of vector, and then calling the output function.

4. Write two functions, called mean and var, that return the mean and variance of the input argument, which should be a vector<double>. So the first of these would be of the form:

double mean(const vector<double>& v){

...

}

The mean of a sample x1, x2, . . . , xn  is

 =    Xxk

and the variance by convention is given as

2 =    1    X (xk − )2

Hint: see the function for the sum on slide 17 of lecture 8.

Also write functions min and max to find the minimum and maximum of a

vector<double>.

Test all of these functions on a vector<double> containing the elements {1, 2, 3, 4, 5}. The mean and variance should be 3 and 2.5 respectively.

5.  Repeat Question 4 using valarray<long double> or valarray<double> (instead of vector<double>).

Hint 1: you may use V*V to square all elements of a valarray V.

Hint 2: you may use X.sum() to compute the sum over all elements of a valarray X. Hint 3: you may use A-B to (elementwise) subtract the elements of a valarray B from the respective elements of a valarray A.

6. Random number generators are important in numerical applications. One of the simpler methods is called the linear congruential method.   Here,  starting with some initial number X0   (which is called the seed) and constants  a, c and m, with all numbers non-negative integers, we can generate a sequence

Xn+1 = (aXn + c) modm

Here, mod is the modulus or remainder operator, which in C++ is %.

Obviously this generates a deterministic sequence, but for good choices of a, c and m the sequence looks like random integers between 0 and m − 1.

One possible choice is m = 134456, a = 8121, c = 28411. Write a function

int randlc(const int x)

that uses these constants and does a single update of the sequence, so the function argument is Xn  and the returned value is Xn+1 .

For some large N , e.g. 10,000, and some positive integer choice for X0, use the function randlc to fill in a vector<int> of size N with the sequence X1 to XN .

Then create a vector<double> of size N and set the elements equal to

X1                  XN   

m − 1 , . . . , m − 1

These values should approximate random numbers from the uniform distribution on the interval 0 to 1. This continuous distribution has mean 1/2 and variance 1/12. Use the functions from question 4 to check the mean and variance, and also check that the minimum and maximum values are close to 0 and 1 respectively.