ELEN3009 - Test 2017
ELEN3009 - Test 2017
ELEN3009 - Test 2017
an
Sch
in g
or
er
m e
ati
on E n gin
Instructions
Question 1
a) Explain why the doctest framework offers an approximate comparison for comparing two
floating point numbers (CHECK(doctest::Approx(left) == right)). (3 marks)
b) Why has the C language’s gets() function been deprecated? (3 marks)
[Total Marks 6]
Page 1 of 4
Question 2
1 int main()
2 {
3 srand(time(0));
4 int num1;
5 int num2;
6 int tot;
7 num1 = (rand()%100)+1;
8 tot = 0;
9
Page 2 of 4
Question 3
a) Write a function, called rotate, which rotates the contents of a vector (of any size) to the
left. This function accepts two iterators marking the range to be rotated. These form the
first and third arguments. The middle argument is an iterator pointing to the element at
which the rotation starts. The behaviour of this function can be inferred from the tests
given in Listing 2.
You may not make use of the STL’s own rotate function in your solution.
Note, the iterators provided by vector support, in addition to the base functionality, pre–
and post–decrementing. They can also be compared to each other using the greater-than
and less-than operators. The first iterator, in an iterator pair specifying a range in a vector,
can be subtracted from the second iterator to give the number of elements in the range.
(12 marks)
b) If an invalid rotation point is supplied to the STL’s version of the rotate algorithm, then
the behaviour of the algorithm is undefined. Why do you think that this is the case rather
than an error being signalled to the calling code? (3 marks)
[Total Marks 15]
Page 3 of 4
TEST_CASE("Rotating vector left from fourth element puts fourth element onwards at
the front") {
vector<int> integers = {1, 2, 3, 4, 5, 6};
TEST_CASE("Rotating vector left from third element puts third element onwards at
the front") {
vector<int> integers = {1, 2, 3, 4, 5};
TEST_CASE("Rotating vector left from last element puts last element at the front") {
vector<int> integers = {1, 2, 3, 4, 5};
Page 4 of 4