#include #include #include struct Cylinder { double radius; double height; }; int main () { std::vector cylinders { Cylinder { 2.5 , 2.3 }, Cylinder { 3.4 , 1.6 }, Cylinder { 1.9 , 4.5 }, Cylinder { 4.0 , 1.5 }, Cylinder { 3.2 , 2.0 }, Cylinder { 4.8 , 1.1 } }; double const minimum_volume {33.0}; /* BEFORE YOU READ THE EXAMPLE SOLUTION: The algorithm all_of is used in this solution, it is likely the most appropriate algorithm for this particular problem. If you formulate the problem as: "Do all of the elements in this container(vector) fulfill the criteria" This algorithm does exactly that. However, we considered many other algorithms to have been appropriate enough for this base-level task. Examples of other ways of solving this problem is: 1. First use transform to fill another vector with the volumes of all cylinders (this would simplify the lambda for the check somewhat) 2. Use any_of or none_of with appropriate negations (a bit less clear cut than all_of) 3. Use find, find_if, find_if_not with appropriate criteria to see if any element does not fulfill the criteria 4. count or count_if to count the number of elements that either fulfill or does not fulfill the criteria in this task. Then check if bigger than 0 or the same length as the vector, depending on how you counted 5. copy_if with an appropriate comparator to copy all elements that match or do not match the criteria to a new container and then comparing the length of the new container (this is just a more convoluted version of count/count_if, would have been okay for this base_level task) 6. using sort to sort the elements in ascending or descending order based on volume, then looking at the last or first element to see if all elements fulfill the criteria These are just examples of okay ways of solving the task we came up with in a short period of time. There are likely more that we would be okay with. The point is that you need to have some familiarity with the stl-algorithm documentation (https://en.cppreference.com/w/cpp/algorithm and the container specific ones) so that you can find an appropriate one. And you need to be able to use them based on that documentation for novel cases. Reading and understanding the solution below is not really a sufficient way of studying for the exam. NOTE: for_each is never an appropriate algorithm. NOTE: Each of the example algorithms can be used in inappropriate ways to solve the task in a manner that would not have been okay. Basically you could use any algorithm that iterates over a container as a for-loop. If you are using e.g. the transform algorithm, but you are not really using it to transform an input range using a function to an output range. Rather using the capture box to manipulate variables to get a convoluted and misleading for-loop, that solution would not be okay. NOTE: Additional constraints are likely to appear for these types of tasks in the second part of the exam. Thus requiring a better familiarity with the documentation and how to apply it. */ bool check { std::all_of(cylinders.begin(), cylinders.end(), [&minimum_volume](Cylinder const& c) { return c.radius * c.radius * 3.14 * c.height > minimum_volume; }) }; if (check) { std::cout << "Ja" << std::endl; } else { std::cout << "nej" << std::endl; } }