Search

link to homepage

Institute for Advanced Simulation (IAS)

Navigation and service


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

You should be able to answer at least some of these questions.

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

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

Q2. Predict the output :

std::list<int> 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=nulllptr;
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(), "Haha!");
}

Q5. The following program is meant to echo the values given to it on the standard input, if the 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;
}
);
}


Servicemeu

Homepage