OOP in C++
Dr Robert Nu ̈rnberg
Driving Test 2013
Friday, 22 March 3.15pm – 5.15pm
Using the account details given below, attempt all 3 tasks. Write one C++ file per task. As your working directory you may choose C:\temp\OOP your name. Make sure to write in the answers to each question on the Answer Sheet. Once you have finished, save the 3 files TaskN.cpp, N=1. . .3, to a directory, e.g. to C:\temp\SUBMIT your name, and contact the invigilator. Do not log off! Hand in your answer sheet and present your submission directory. The invigilator will then reconnect the network cable so that you can email your solutions to rn@ic.ac.uk with a CC to yourself. You are then free to leave.
Username : ***** Password : *****
1. Word score [30 marks]
Let M = {A,B,C,…,Z} denote the alphabet. Let g : M → N denote the letter score, which is defined by
g(A)=1, g(B)=2, g(C)=3, …, g(Y)=25, g(Z)=26. Now for an n-letter word w = (w1,…,wn) ∈ Mn we define the word score
n
s(w) = (−1)i+1 i g(wi) = g(w1) − 2 g(w2) + 3 g(w3) − . . . .
i=1
For example, s(”Y OU”) = 58 and s(”ME”) = 3.
Write a program that can compute the word score for arbitrary words in Mn, n ∈ N. [Hint: The statement cout << (int) (’C’ - ’A’); outputs 2 on screen.]
2. Quaternions in R4 [30 marks]
The real vector space R4 (with the usual vector addition) can be equipped with a noncommu- tative multiplication to yield an algebra over R that is almost a field, and which is denoted by H.
In particular, for a = (a1,a2,a3,a4) ∈ H and b = (b1,b2,b3,b4) ∈ H we define ab ∈ H by ab = (a1b1 − a2b2 − a3b3 − a4b4,
a1b2 + a2b1 + a3b4 − a4b3, a1b3 − a2b4 + a3b1 + a4b2, a1b4 +a2b3 −a3b2 +a4b1).
Note that H can be thought of as an extension of the complex numbers C, i.e. formally R ⊂ C ⊂ H, and so it is common to introduce the notations
1 = (1,0,0,0), i = (0,1,0,0), j = (0,0,1,0), k = (0,0,0,1). With these definitions it then holds that
i2 =j2 =k2 =ijk=−1. Moreover, the conjugate a∗ of a ∈ H is defined by
a∗ =−1 (a+iai+jaj+kak)∈H, 2
which, on noting that a∗a = aa∗ ∈ R≥0, yields the usual definition of the norm of a as ∥a∥=√a∗a=√aa∗ =a21 +a2 +a23 +a24.
Finally, the inverse a−1 = 1/a of a ∈ H is defined by a−1= 1 a∗.
∥a∥2
Note that a/b is not well-defined, since in general ab−1 ̸= b−1a.
Write a program that can perform numerical computations for quaternions in H.
OOP in C++ Dr Robert Nu ̈rnberg
3. Quaternions in Z4 and C4 [40 marks]
On ignoring the definitions of the norm and the inverse, quaternions can also be introduced over the fields Z and C. Write a program that allows computations for such numbers using templates. In particular, your code should execute the following statements correctly.
quaternion
c = a*a – b*b;
complex
quaternion
cc = aa*aa – bb*bb;
[Hint: You should include the line #include
OOP in C++
Friday, 22 March 3.15pm – 5.15pm
Name
CID
Answer Sheet A
1. State the following word scores.
2. Let a = − 1 + 3i + k and b = 5j − 2k. State the values of the following expressions. Display 4
all values to 4 decimal digits.
word
score
ZEOULOUS
TRANSPORTER
LECTURERS
value
iaj bk
a2 − b2
(a+b)∗
a−1
b−1 a
ab−1
3. Let a = (123456789, 12345, 54321, 987654321) ∈ Z4 and b = (−999, 111, 222, 333) ∈ Z4. State the last two digits of the entries of the following expressions.
Leta=(1+i,−5,2−3i,i)∈C4 andb=(0,5i,1−i,8+9i)∈C4. Statethevaluesofthe following expressions.
value (last two digits of each entry)
ab
ba
a2 − b2
value
ab
ba
a2 − b2