OOP in C++
Dr Robert Nu ̈rnberg
Driving Test 2017
Wednesday, 22 March 2pm – 4pm
Using the account details given below, attempt all 4 tasks. Write one C++ file per task. Make sure to fill in the answers to each question on the Answer Sheet. Also make sure that the codes you submit will produce all the answers from your answer sheet when compiled and run. Once you have finished, save the 4 files taskN.cpp, N=1…4, to a directory or to the Desktop, and contact the invigilator. Do not log off! Hand in your answer sheet and present your files. The invigilator will then reconnect the network cable so that you can email your solutions to robert.nurnberg@imperial.ac.uk with a CC to yourself. You are then free to leave.
Username : ******* Password : *******
1. Multiples [20 marks]
Let p,q ∈ N and let Sp,q(N) denote the sum of all the positive integers smaller than N that are multiples of p or q. For example,
S3,5(10) = 3 + 5 + 6 + 9 = 23 . Write a program that can compute Sp,q(N) for given p,q,N ∈ N.
2. Base notation [30 marks]
For z,b ∈ N it holds that z = (dn···d1d0)b in base-b notation, if the digits satisfy di ∈ {0,1,…,b−1}foralli=0,…,n,withdn ̸=0ifn>0,andif
n
z = bi di .
i=0
Of course, we are very familiar with this notation in the case b = 10. In order to save space
for b > 10, we replace the digit values greater than 9 with the letters A, B, C etc, with A having value 10, B value 11 and so on. For example, 11 = (1011)2 and 31 = (1F)16.
Write a program that can compute z = (dn · · · d1d0)b for given z, b ∈ N.
Hint: The following code produces a vector of digits for the base-10 notation of z ∈ N:
vector
while (z != 0) {
digits.push_back(z % 10);
z /= 10; }
OOP in C++ Dr Robert Nu ̈rnberg
3. Full matrix templates [30 marks]
For arbitrary square matrices A,B ∈ Kn×n and α ∈ K implement a class template fullmatrix
• α A (or A α, or operator*=), • A + B (or operator+=),
• A B (or operator*=),
• tr(A) = ni=1 Aii.
Write a program that can perform the above operations in the cases where K is modelled by int, double or complex
4. Matrix polynomials [20 marks]
Provide a simple class template polynomial
Write a program that can compute p(x) for a given polynomial and given x ∈ K, and similarly p(A) for A ∈ Kn×n.
[Hint: Apart from this evaluation of the polynomial for a scalar, or for a matrix, you do not need to implement any other operation or feature for this simple polynomial class.]
OOP in C++
Wednesday, 22 March 2pm – 4pm
Name
CID
1.
2.
Answer Sheet A
value
S4,7 (1000)
S11,13 (10000)
dn ···d1d0
2017 = (dn ···d1d0)6
20170322 = (dn · · · d1d0)18
3. Let Hn ∈ Rn×n be defined by (Hn)ij = (i+j−1)−1. Let An ∈ Zn×n be defined by (An)ij = (−1)i+j (i + j). Let Zn ∈ Cn×n be defined by Zn = Hn + i An. In particular, for thecasen=1wehavethatH1 =(1),A1 =(2)andZ1 =(1+2i).
State the following values to two decimal digits, i.e. 123.456789 ≈ 123.46.
value
tr(A54 A54)
H5 A5 + 2 A5 H5
tr(H99 Z99)
4. Let Zn as before. Define pm(x) = mi=0 αi xi, where i+1 i+1
αi = (−1) 2 2 i is odd, 0 i is even.
State the following values to two decimal digits, i.e. 123.456789 ≈ 123.46.
value
p1000(1 + i) 42
Z 3 3 + i Z 32 + Z 3
p110(1 0.5) 0i