Final

Contents

Final#

C++#

  1. Ranges and iterators.

    For these questions, you have a `` std::vector`` named vec:

    1. vec.begin() points to the first element of the vector. What does vec.end() point to?

      solution

      vec.end() points to one-past the last element in vec.

    2. Consider the line: auto it = std::ranges::max_element(vec);

      1. What name did we use for the concept that it represents?

        solution

        We called it an iterator

      2. How do I get the value of the maximum element from it?

        solution

        You need to dereference it, i.e., *it.

  2. Lambda-functions. This line tests if any element in a std::vector<int> named vec is 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);
    
  3. Classes. Consider the following class:

    class Thing {
        double x;
        double y;
    
        Thing(double _x, double _y) {
            x = _x;
            y = _y;
        }
    };
    
    1. 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

    2. If you do Thing t(1, 2); in your main function, 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 change class to struct.

  4. Operators. In our mathematical vectors Vector2d class, 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 Vector2d objects, u and v, 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 - v will call the first and doing -u will call the second.

  5. Consider the following class Roster that 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
    };
    
    1. Write a member function for Roster named add_student that takes a name, id, and grade (of the types defined in Student) and adds the student to the students vector.

      solution

      void add_student(std::string name, int id, double grade) {
          Student s{.name=name, .id=id, .grade=grade};
          students.push_back(s);
      }
      
    2. Write a member function for Roster that returns the number of students.

      solution

      int num_students() {
          return students.size();
      }
      
  6. Software engineering.

    1. You have a directory with multiple .cpp files and headers, and there is a GNUmakefile. What single command could you type to compile and link all the sources?

      solution

      make

    2. What tool did we cover in class that allows you to keep track of changes to a software project?

      solution

      git

    3. I have a header file myheader.H in my directory. For the .cpp file 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#

  1. 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}")
    
  2. 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)
    
  3. What is the python equivalent of:

    1. auto y = std::pow(x, 4);

      solution

      y = x**4
      
    2. 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#

  1. Tell me something that you learned in class that you enjoyed.