ELEN3009 - Test 2017

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

le

of E ctrica UNIVERSITY OF THE WITWATERSRAND, JOHANNESBURG


ol l
o

an
Sch

School of Electrical and Information Engineering


d ELEN3009 – Software Development II
Inf

in g
or

er
m e
ati
on E n gin

September Test 2017: 1 Hour – 35 marks

Instructions

• Answer all questions. The questions do not carry equal weight.


• For questions which require you to write source code, note that:
– You only need to specify #include’s if specifically asked.
– For classes, you can give the implementation entirely in the header file, unless
directed otherwise.
– Marks are not awarded solely for functionality but also for good design, mak-
ing appropriate use of library functions, following good coding practices, and
using a modern, idiomatic C++ style.
– Your code must be easily understandable or well commented.
– You may use pencil but then you forfeit the right to query the marks.
• Reference sheets are provided separately.

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

Examine the code below and answer the following questions:


a) What does this program do? (1 marks)
b) This program does not conform with good coding principles and practices. Identify at least
five different issues that it has. (5 marks)
c) Refactor the code to improve its structure. You should make use of one or more functions
in your solution. (8 marks)

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

10 cout<< "Let the game begin... " << endl;


11 for (int i = 1; i < 6; i++)
12 {
13 cin >> num2;
14 tot++;
15 if (num2 != num1)
16 {
17 if (tot == 5)
18 {
19 cout << "Sorry, you lose." << endl;
20 }
21 else
22 {
23 if (num2 > num1)
24 cout<< "Guess smaller." << endl;
25 else if (num2 < num1)
26 cout << "Guess bigger." << endl;
27 }
28 }
29 else
30 {
31 cout<< "Congratulations, you win!" << endl;
32 break;
33 }
34 }
35 return 0;
36 }

Listing 1: Poorly structured program

[Total Marks 14]

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};

rotate(begin(integers), begin(integers) + 3, end(integers)) ;

vector<int> expected = {4, 5, 6, 1, 2, 3};


CHECK(integers == expected);
}

TEST_CASE("Rotating vector left from third element puts third element onwards at
the front") {
vector<int> integers = {1, 2, 3, 4, 5};

rotate(begin(integers), begin(integers) + 2, end(integers)) ;

vector<int> expected = {3, 4, 5, 1, 2};


CHECK(integers == expected);
}

TEST_CASE("Rotating vector left from first element leaves vector unchanged") {


vector<int> integers = {1, 2, 3, 4, 5};

rotate(begin(integers), begin(integers), end(integers)) ;

vector<int> expected = {1, 2, 3, 4, 5};


CHECK(integers == expected);
}

TEST_CASE("Rotating vector left from last element puts last element at the front") {
vector<int> integers = {1, 2, 3, 4, 5};

rotate(begin(integers), end(integers) - 1, end(integers)) ;

vector<int> expected = {5, 1, 2, 3, 4};


CHECK(integers == expected);
}

TEST_CASE("Rotating vector left with invalid rotation point leaves vector


unchanged") {
vector<int> integers = {1, 2, 3, 4, 5};

rotate(begin(integers), end(integers) + 2, end(integers)) ;

vector<int> expected = {1, 2, 3, 4, 5};


CHECK(integers == expected);
}

Listing 2: Tests for the rotate function

Page 4 of 4

You might also like