Course topic 7 exercises

Note

These are the exercises for week 7. The deadline for submitting your answers this week is after the period break. Before you start working on the exercises, make sure you have pulled the latest updates from the course repository:

  • git pull course-upstream main

How would you perform the following tasks using STL algorithms? These exercises are returned with git commit HASH. First, pull the latest updates from course-upstream, implement the missing algorithms/functions in the eight files wk03_stl/stl/stl1.cc through wk03_stl/stl/stl8.cc one file for each algorithm.

In all the exercises, you need to use STL algorithms to manipulate the containers and return EXIT_SUCCESS. The only situations where EXIT_FAILURE would be returned would be any time when the STL algorithm itself fails (which should not happen if you use it correctly).

Opening the project in Qt Creator (In remote desktop server)

These instructions are directed to those who are using the remote desktop server. If you are using your own computer, some of the instructions may not apply to you depending on the version of Qt Creator you are using.

As a reminder, tuni offers the remote desktop server for all students. Follow instructions for obtaining user rights and installation process here: https://www.tuni.fi/en/it-services/handbook/networks/linux-ssh-and-remote-desktop-servers

  1. Open the remote desktop server and log in. Clone or update your git repository if you haven’t already done so in the remote desktop server.
  2. Open Qt Creator in the remote desktop server.
  3. To open the project in Qt Creator, you need to open the project file (wk03_stl/stl/stl.pro in this case) in Qt Creator by selecting File->Open File or Project. Then navigate to the stl.pro file in the repository and open it.
  4. QT Creator will ask you to configure the project. If the build&run kit Desktop Qt 6.1.1 GCC 64bit is already checked, you can click Configure Project which will open the project.

Testing your functions

The project contains test cases for all the exercises that can be locally run with command line arguments.

Changing the command line parameters in Qt Creator

To change the command line parameters in Qt Creator, go to the Projects tab on the left side of the window. Then select the Run tab. You can then change the command line parameters in the Arguments field (Command line arguments).

Examples of command line parameters

As an example, if you want to run the test with the data-size of 10 for STL-algorithm 1, you need to add the following command line arguments:

1 10

In STL-algorithm 3, you need to use the following command line argument to search for the value 5 from a vector of size 10:

3 10 5

Attention

In these following exercises you are supposed to use the functions and algorithm provided by the STL libraries and therefore using any regular for and while loops are not allowed. However, using std::for_each is allowed.

You can use cplusplus.com’s algorithm pages or similar resources to help you.

Exercises utilize following two STL containers:

std::vector<int> v;
std::map<std::string, int> m;

STL-algorithm 1, sort asc

Sort a given vector to an ascending order.

Your answer should be in the file wk03_stl/stl/stl1.cc

A+ presents the exercise submission form here.

STL-algorithm 2, sort desc

Sort a given vector to a descending order.

Your answer should be in the file wk03_stl/stl/stl2.cc

A+ presents the exercise submission form here.

STL-algorithm 3, find a given value

Return an iterator which points to the first instance of the given value. If the value cannot be found return v.end() instead.

Your answer should be in the file wk03_stl/stl/stl3.cc

A+ presents the exercise submission form here.

STL-algorithm 4, find the last even value

Return an iterator which points to the last even integer of the vector. If there are no even values return v.rend() instead.

Your answer should be in the file wk03_stl/stl/stl4.cc

A+ presents the exercise submission form here.

STL-algorithm 5, sort mod 3

Sort a vector in three subsequent sections:
  • those divisible by three (ascending order)
  • those whose reminder is 1 (ascending order)
  • those whose reminder is 2 (ascending order)

Your answer should be in the file wk03_stl/stl/stl5.cc

A+ presents the exercise submission form here.

STL-algorithm 6, find value from a map

From a map find an element, whose value is at least as large given value regardless of the key of the element. Note that map elements are of type std::pair<std::string, int>, and the key can be accessed through the .first field and the value through the .second.

Return only the found value or NOT_FOUND (constant defined in test.hh) if none of the values match the search criteria.

Your answer should be in the file wk03_stl/stl/stl6.cc

A+ presents the exercise submission form here.

STL-algorithm 7, find median

Find the median value of a given vector, whose elements are in random order. Return NOT_FOUND (constant defined in test.hh) if the size of the vector is zero.

Read more about median and how to calculate it:

NOTE Whenever the median value is returned, round the value down to the closest integer.

Your answer should be in the file wk03_stl/stl/stl7.cc

A+ presents the exercise submission form here.

STL-algorithm 8, remove values

Remove from a vector v all elements with value less than the given limit.

Your answer should be in the file wk03_stl/stl/stl8.cc

A+ presents the exercise submission form here.

Posting submission...