M1103 Mini-Set 1

Before you begin: activate C++14

Problem 1 — Using Vectors

The official documentation is one of the only places that contain definitive information on (almost) every aspect of C++. This official documentation is preferable to many tutorials that can be found on the web, also because many tend to be out-of-date for recent versions of C++.

In this problem we will experiment with using the C++ class vector. It will be useful to read the official documentation of the class vector, in particular as a future reference.

Take a particular look at the documentation of the functions

which we will use in this problem.

Download the file code.zip and add its content (the files vectors.cpp, vectors.h, test.cpp, asserts.h) to a new Code::Blocks project.

a — Vector of the \(n\) First Nonnegative Integers

It is possible to create an empty vector of integers vec with the instruction vector<int> vec. While we can create a vector with the elements \(0,1,2,3\) by writing vector<int> vec = {0,1,2,3}, this is not sufficient to create a vector with a non-constant number of elements. We thus need to create an empty vector first, to which we then add elements inside a loop.

Add the necessary instructions to the range function in the file vectors.cpp. This function takes as a parameter an integer \(n\) and returns a vector whose elements are \(0,1,2,\dots,(n-1)\), i.e., the first \(n\) nonnegative integers in increasing order. If the parameter \(n\) is smaller or equal to \(0\), then the range function returns an empty vector of integers.

You can test your function by executing the program whose main function is defined in the file test.cpp.

b — Return the First Element of a Vector

Hint: Carefully look at the behavior of the at function of the vector class in case of an out-of-bounds index.

Add the necessary instructions to the firstElement function in the file vectors.cpp. This function takes as a parameter (a constant reference to) a vector of integers and returns its first element. If the vector does not have a first element (because it is empty), the function should throw an exception.

You can test your function by executing the program whose main function is defined in the file test.cpp.

c — Delete the First Odd Element

Be careful to exit the function (or at least the loop) as soon as you find the first odd element. In general, it is not a good idea to continue to loop over the elements of a vector once it has changed.

Note that the erase function does not take an int as a parameter to specify the to-be-deleted element; it takes a position which can be specified relatively to the start of the vector. To delete the \(i\)th element of the vector vec, we hence write vec.erase(vec.begin() + i).

Add the necessary instructions to the deleteFirstOdd function in the file vectors.cpp. This function takes as a parameter (a reference to) a vector of integers and deletes its first odd element, if there is one. If not, the vector should not change.

You can test your function by executing the program whose main function is defined in the file test.cpp.

back to the course web site