程序代写代做 AI algorithm C c++ data structure OOP in C++

OOP in C++
Dr Robert Nu ̈rnberg
Driving Test 2011
Wednesday, 23 March 2pm – 4pm
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 : ******
For some of the tasks you need to #include for the functions sqrt(), exp() and log(). 1. Prime number sums [30 marks]
Let pk denote the kth prime number for k ≥ 1. Define the finite sums nn
Sn =􏰂pk =2+3+…, Sn± =􏰂(−1)k+1pk =2−3+…,
k=1 k=1 nn
􏰂1 1 1
Tn= p =2+3+…,
± 􏰂(−1)k+1 1 1
Tn = p =2−3+….
k=1k
Write a program that can compute Sn, Sn±, Tn and Tn± for arbitrary n ≥ 1.
2. Classical Gram–Schmidt [30 marks]
Let ⟨·, ·⟩ denote the standard inner product on Rm; i.e. ⟨a, b⟩ = aT b = 􏰁mi=1 ai bi for all
m1m a, b ∈ R . Let ∥a∥ = (⟨a,a⟩)2 . Given n linearly independent vectors a1, a2,…,an ∈ R ;
m ≥ n; the classical Gram–Schmidt algorithm is defined as follows: v1 = a1
q1 = v1/∥v1∥ for i = 2 to n
i−1
vi =ai −􏰂⟨ai,qj⟩qj j=1
qi = vi/∥vi∥
end
Hence the algorithm computes the vectors q1, q2, . . . , qn ∈ Rm. It is easy to show that for linearly independent {ai}ni=1 the algorithm is well-defined and that the vectors {qi}ni=1 are orthonormal and span the same subspace as {ai}ni=1. Defining the matrices A = [a1,a2,…,an] ∈ Rm×n and Q = [q1,q2,…,qn] ∈ Rm×n in terms of their columns, the classical Gram–Schmidt algorithm can also be interpreted as transforming A into Q.
Write a program that performs the classical Gram–Schmidt algorithm on the given set of vectors {ai}ni=1 and then returns {qi}ni=1.
[Note: There is no restriction on how you implement the algorithm, as long as it works for arbitrary dimensions m ≥ n. For instance, you might prefer to have your program work on the rows of the matrix AT ∈ Rn×m and then return QT . Moreover, you may assume that the given vectors {ai}ni=1 are indeed linearly independent. You do not need to perform any well-posedness or consistency checks in your program.]
k=1 k

OOP in C++ Dr Robert Nu ̈rnberg
3. Step functions [40 marks]
For a, b ∈ R with a < b the characteristic function χ[a,b) : R → K is defined by 􏰀1 a≤x >. Overload
the function call operator to return s(x) for x ∈ R and implement a member function
intR() which returns the intregral 􏰋 s(x) dx. Overload the addition operator to R
compute the sum between two step functions.
(c) Write a global template function interpolate that, given a function f and a parti-
tioning of an interval [a,b], computes the interpolating step function sfn from (2). In addition, provide the functions uniform_partition and logarithmic_partition that for n ≥ 1 compute the corresponding partitions of a given interval [a, b].
(d) Overload the multiplication operator step_function::operator*() three ways: for the multiplication with a scalar β ∈ K, with a generalized characteristic function and with another step function.
[Hint: The member function vector::clear() may or may not prove useful.]

OOP in C++
Wednesday, 23 March 2pm – 4pm
Name
CID
Answer Sheet A
1. To five decimal places, state the values of the following sums.
Sum
Value
S50
S± 100
T200
T± 250
Note: For N = 10 the numbers are: SN = 129, SN± = −13, TN = 1.53344, TN± = 0.25298.
2. To three decimal places, state the matrix Q that your program computes for the given
matrices A.
A
Q

2 14
 
 3 0 
 13 23 

1 0 −1 2 

01 1 0 
00 3−3 

32−1 1 

0001

1 2 0.447 0.894
   􏰈22􏰉
Note: ForA=2 2theresultisQ≈0.894 −0.447,whileforA= 02 the 0000
result is Q = 􏰈1 0􏰉. 01

OOP in C++ Wednesday, 23 March 2pm – 4pm
3. Write a program that executes the following statements correctly.
double f(double x);
double f2(double x) { return f(x)*f(x); }
int main() {
int n = 100;
double a = -0.5; double b = 0.5;
vector partuni, partlog;
step_function s, t, u, v, w;
uniform_partition(a, b, n, partuni);
interpolate(f, partuni, s);
cout << "Integral with uniform = " << s.intR() << endl; logarithmic_partition(a, b, n, partlog); interpolate(f, partlog, t); cout << "Integral with logarithmic = " << t.intR() << endl; u = s + t; cout << "u(0.1) = " << u(0.1) << endl; cout << "Integral sum = " << u.intR() << endl; v = s * t; cout << "Integral of s * t = " << v.intR() << endl; interpolate(f2, partlog, w); cout << "Integral of f^2 with logarithmic = " << w.intR() << endl; return 0; } where your implementation of the function f() returns the value of f(x) = exp(x2 + for x ∈ R. Below fill in the outputs of your program. 1 ) 1+x Line Output cout << "..." << s.intR() << endl; cout << "..." << t.intR() << endl; cout << "u(0.1) = " << u(0.1) << endl; cout << "..." << u.intR() << endl; cout << "..." << v.intR() << endl; cout << "..." << w.intR() << endl; Finally, on including #include, replace step_function with step_function > and let f(x) = exp(ix2 + 1 ) ∈ C for x ∈ R. The
1+x
imaginary unit i can be defined as complex i(0.0, 1.0);, while the complex
exponential function is also called exp(). What are the outputs now?
Line
Output
cout << "..." << s.intR() << endl; cout << "..." << t.intR() << endl; cout << "u(0.1) = " << u(0.1) << endl; cout << "..." << u.intR() << endl; cout << "..." << v.intR() << endl; cout << "..." << w.intR() << endl;