Navigation and service

Self evaluation questions for the course "High Performance Computing in C++"

Assume C++ language standard C++17 for all of the following questions.
These questions are for self-evaluation. You need not be able to answer all of them, but at least 50%. If not, you will be better served by an introductory course on contemporary C++.

Q1. What are the sums of the elements in the containers created by the following two declarations ?

std::vector v{1,2};
std::vector w(1,2);

Q2. Predict the output :

std::list L{1, 1, 2, 3, 5, 8, 13};
L.sort([](auto i, auto j) { return i > j; });
for (auto a : L) std::cout << a << "\n";

Q3. What do you need to add in class C to make the code in function f() correct ?

class C {
double * data = nullptr;
size_t N=0;
public:
C() = default;
~C() { if (data) delete [] data; }
C(size_t n) : data{new double[n]}, N{n} {}
size_t size() const { return N; }
double operator[](size_t i) const { return data[i]; }
};

void f(C & v)
{
for (size_t i = 0; i < v.size(); ++i) {
v[i] = 5 * i * i + 2;
}
}

Q4. On a machine with int, long, float and double of sizes 4, 8, 4, and 8 bytes respectively, will this compile ? (Hint: there are no syntax errors!)

#include <array>

template <int m, int n>
struct smaller_impl
{
static constexpr int val = m < n ? m : n;
};
template <int m, int n>
constexpr int smaller = smaller_impl<m,n>::val;

template <typename A, int m, int n>
class C {
std::array<A, smaller<m,n>> data;
public:
constexpr auto size() const { return data.size(); }
};

int main()
{
C<int, sizeof(long), sizeof(double)> c;
C<int, sizeof(int), sizeof(float)> d;
static_assert(c.size() < d.size());
}

Q5. The following program is meant to echo the values given to it on the standard input, when the latest input value is greater than all previous inputs. It does not compile because of one single missing keyword. Can you detect and fix it ?

#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;
int main()
{
copy_if(istream_iterator<size_t>{cin},
istream_iterator<size_t>{},
ostream_iterator<size_t>{cout, " <--- new max.\n"},
[prevmx=0ul](auto n) {
if (n > prevmx) {
prevmx = n;
return true;
} else return false;
}
);
}

Q6. The following program tests a function which should print out its arguments separated by spaces. The function takes an arbitrary number of arguments. The aim is to print string arguments, like names, message texts etc. enclosed inside quotation marks, while printing int, double etc without any additional decoration. The program compiles and runs, but does not quite work as intended. Can you fix it ? Hint: at most one line needs to be changed.

#include <iostream>
#include <string>
#include <iomanip>
#include <type_traits>

template <class ... Args>
void print_all(Args && ... args)
{
using namespace std;
auto proc = [](const auto & x) {
if constexpr (is_same_v<decltype(x), string>)
cout << quoted(x) << " ";
else
cout << x << " ";
};
(proc(args) , ...);
cout << "\n";
}

int main()

{
using namespace std::string_literals;
auto i=3, j=9;
auto x = 8.2;
std::string name = "Super Nova";
print_all("How many fields did I print in this line ?"s);
print_all(x, j, "nine", i, name);
print_all(1ul, "message one"s);
}