Programming Paradigms - C++ FS 2017: Universit at Basel
Programming Paradigms - C++ FS 2017: Universit at Basel
Thorsten Möller
Florian Spiess - f.spiess@unibas.ch
Upload answers to the questions and source code before the delivery deadline
via courses.cs.unibas.ch. In addition, running programs have to be demonstrated
during the exercise slots until Friday, 24.04.2017 the latest. Also note, of all
mandatory exercises given throughout the course, you must score at least 2/3 of
their total sum of points to get accepted in the final exam.
int a = 2;
int b = 3;
int *p = &a, **q = &p;
*p *= 4+*&b***q; // Never write such minified lines in your code!
// This is just for the sake of it.
p = &b;
(*p)++;
p += 10;
cout << a << " " << b << " " << p << " " << (p - (p - 10)) << endl;
1
Question 2: Pointers (8 points)
a) The following C++ code is supposed to retrieve an address value created within the
function foo. Does that make sense? Explain your answer in any case.
int* foo() {
int x = 3;
int p* = &x;
return p;
}
int main() {
int* print_out = foo();
cout << print_out << endl;
}
(2 points)
b) Write a C++ function that takes an array of arbitrary size prepends the contents
of the array in reverse order to the front of the array and returns the pointer to the
beginning of the new array. Write a main function to test your implementation.
Example: Let’s say that we get the array [1, 2, 3] as input. Then we get the pointer
to the first element of the following array as output [1, 2, 3, 3, 2, 1].
(2 points)
c) Consider your answer to b). Is it possible to write a reliable function fulfilling the
requirements of b) so the pointer to the first element of the new array is the same
as the pointer to the first element of the original? If yes, implement a function that
guarantees this and explain your implementation. If not, explain why.
Please give detailed explanations.
(2 points)
d) The following C++ fragment contains some errors. Find, explain and fix them.
int a = 1, b = 1;
int* p1, p2;
p1 = &a;
p2 = &b;
if (p1 == *p2) {
cout << "a != b" << endl;
} else {
cout << "a == b" << endl;
}
(2 points)
2
Question 3: Function Pointers (6 points)
This exercise is about function pointers. In practice you often need to define an inter-
face that can use different functions within an algorithm. Design an algorithm that can
compare two arrays of the same length according to different comparator functions. More
precisely, the algorithm takes two double arrays of size 3 and a comparator function
as input and returns whether one array is larger than the other in regard to the given
comparator; that is, return either of −1, 0, 1 if the first is smaller, equal, or larger than
the second one.
Implement two comparators: One that compares the two arrays according to their 3rd
element. The other comparator considers an array to represent a point in the Euclidian
space R3 and compares the points (arrays) according to the Euclidean distance (see http:
//en.wikipedia.org/wiki/Euclidean_distance) from the point of origin (0, 0, 0); i.e.,
the first point is larger than the second if it is more distant from (0, 0, 0) than the second.
Use function pointers to pass the comparator function into the function.
bool swap(vector& v1, vector& v2); //Swaps the values for both vectors.
bool equals(vector& v1, vector& v2); //Do both vectors contain the
//same values in the same order?
(7 points)
3
b) In this exercise you will implement the stooge sort algorithm for our vector data
structure just created. Stooge sort is a recursive sorting algorithm with relatively bad
sorting performance (see https://en.wikipedia.org/wiki/Stooge_sort), but it is
a very interesting algorithm conceptually. It works on basis of a few simple rules:
1. If the value at the end is smaller than the value at the beginning of the given
section of the list, swap them
2. If the considered section of the list contains three elements or more:
a) Stooge sort the first 2/3 of this section of the list
b) Stooge sort the last 2/3 of this section of the list
c) Stooge sort the first 2/3 of this section of the list
It is important to round up rather than down when calculating the 2/3 of the re-
maining list.
Important: The implementation should be as memory-efficient as possible. This
means we always work on the same structure.
(5 points)