Apr 10 In-Class

Apr 10 In-Class#

Note

There are no wrong answers. This is for participation credit, and is intended to let me know how well we understand the material at this point.

Access the form: https://forms.gle/8zfGbH52q3EBKvZF7

  1. Consider the following:

    double x{10.0};
    double *p = &x;
    

    How do we see the value in the memory location that p is pointing to?

    solution

    We deference it as *p.

  2. Consider the following—what do you think the output printed to the screen is?

    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    bool is_even(int e) {
        return e % 2 == 0;
    }
    
    int main() {
    
        std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8};
        auto val = std::ranges::count_if(vec, is_even);
    
        std::cout << val << std::endl;
    }
    
    solution

    This will only count the elements in the vector for which is_even returns true, so we will get 4.