Final#
C++#
Ranges and iterators.
For these questions, you have a `` std::vector`` named
vec:vec.begin()points to the first element of the vector. What doesvec.end()point to?solution
vec.end()points to one-past the last element invec.Consider the line:
auto it = std::ranges::max_element(vec);What name did we use for the concept that
itrepresents?solution
We called it an iterator
How do I get the value of the maximum element from
it?solution
You need to dereference it, i.e.,
*it.
Lambda-functions. This line tests if any element in a
std::vector<int>namedvecis even:auto result = std::ranges::any_of(vec, [] (int e) {return e % 2 == 0;});
Here we provide the function that tests each element as a lambda function. Rewrite this by writing a separate function to do this test, and show how the
std::ranges::any_of()call is modified to call your function.solution
bool is_even(int e) { return e % 2 == 0; } std::ranges::any_of(vec, is_even);
Classes. Consider the following class:
class Thing { double x; double y; Thing(double _x, double _y) { x = _x; y = _y; } };
Both the class and a function inside the class are named
Thing—what special name do we give that function in the context of object-oriented programming?solution
We called it the constructor
If you do
Thing t(1, 2);in yourmainfunction, you get the compilation error:error: ‘Thing::Thing(double, double)’ is private within this context
How can you fix this? (there are a few ways, be specific about what change you are suggesting).
solution
You can either add
public:to the body of the class (just before the constructor), or you can changeclasstostruct.
Operators. In our mathematical vectors
Vector2dclass, we had the following two operators for \(-\):Vector2d operator-(const Vector2d& vec) { return Vector2d(x - vec.x, y - vec.y); } Vector2d operator-() { return Vector2d(-x, -y); }
What is the difference between these? Given two
Vector2dobjects,uandv, give an example of operations that would call each of these-operators.solution
The first is subtraction and the other is the unary minus (negation).
Doing
u - vwill call the first and doing-uwill call the second.Consider the following class
Rosterthat represents a classroom of students:struct Student { std::string name; int id{}; double grade{}; }; struct Roster { std::vector<Student> students; Roster() {} // add your functions here };
Write a member function for
Rosternamedadd_studentthat takes a name, id, and grade (of the types defined inStudent) and adds the student to thestudentsvector.solution
void add_student(std::string name, int id, double grade) { Student s{.name=name, .id=id, .grade=grade}; students.push_back(s); }
Write a member function for
Rosterthat returns the number of students.solution
int num_students() { return students.size(); }
Software engineering.
You have a directory with multiple
.cppfiles and headers, and there is aGNUmakefile. What single command could you type to compile and link all the sources?solution
makeWhat tool did we cover in class that allows you to keep track of changes to a software project?
solution
git
I have a header file
myheader.Hin my directory. For the.cppfile in the same directory, can I use#include <myheader.H>and#include "myheader.H"interchangeably? Why or why not?solution
They are not interchangeable. You need to do
#include "myheader"—the version with quotes will first look in the current directory for the header file.
Python#
How would you rewrite this C++ code in python using an f-string?
double x{2.65}; double y{1.2e-16}; std::cout << std::format("x = {:5.3f}, y = {:9.4g}", x, y) << std::endl;
solution
x = 2.65 y = 1.2e-16 print(f"{x = {x:5.3f}, y = {y:9.4g}")
Rewrite this C++ code in python:
std::vector<int> vec{1, 5, -2, 13, 8, 9, 4, 12}; std::vector<int> divisible{}; for (auto e : vec) { if (e % 3 == 0) { divisible.push_back(e); } }
solution
vec = [1, 5, -2, 13, 8, 9, 4, 12] divisible = [] for e in vec: if e % 3 == 0: divisible.append(e)
What is the python equivalent of:
auto y = std::pow(x, 4);solution
y = x**4
auto y = std::sin(std::numbers::pi * x / 180);solution
y = math.sin(math.pi * x / 180)
or
y = math.sin(math.radians(x))
Everything#
Tell me something that you learned in class that you enjoyed.