Chapter 14 - Worked Exercises 3

Exercise 1 - Utility Template Functions

Part 1

Write a template function to find the minimum value contained in a vector of any type.

int main() {
    std::vector<int> integers = {5, 1, 4, 3, 2};
    auto min_int = minimum(integers); // Returns 1
    
    std::vector<float> floats = {4.12, -1.3, 0.49, 1.31};
    auto min_float = minimum(floats); // Returns -1.3
}

Part 2

Write a template function to get the sum of values in a vector of any numerical type:

int main() {
    std::vector<int> integers = {5, 1, 4, 3, 2};
    auto sum_int = sum(integers); // Returns 15
    
    std::vector<float> floats = {4.12, -1.3, 0.49, 1.31};
    auto sum_float = sum(floats); // Returns 4.62
}

Part 3

Write a template function to get the average of values in a vector of any numerical type:

int main() {
    std::vector<int> integers = {5, 1, 4, 3, 2};
    auto avg_int = average(integers); // Returns 3
    
    std::vector<float> floats = {4.12, -1.3, 0.49, 1.31};
    auto avg_float = average(floats); // Returns 1.155
}

Part 4

Create a template class that encapsulates the above functions into a class called Calculator.

The below should illustrate the use of the class:

int main() {
    Calculator<int> int_calculator;
    int_calculator.push_back(5);
    int_calculator.push_back(1);
    int_calculator.push_back(4);
    int_calculator.push_back(3);
    int_calculator.push_back(2);
    
    auto min_int = int_calculator.minimum();
    auto sum_int = int_calculator.sum();
    auto avg_int = int_calculator.average();
}

Part 5 - Bonus

Can you get the Calculator template class to work with the Rational class that you made in Chapter 12 - Worked Exercises 1 - Exercise 1?

Identify which operators you are missing, and implement them.

Exercise 2 - Generic Vector

Refer to Chapter 12 - Worked Exercises 1 - Exercise 1, where you've made a vector class that stores an collection of integers.

Using the same class that you have made, adjust it such that it becomes a template class that can hold elements of any type.

Exercise 3 - Linked List

You will find referring to Chapter 9 - Worked Exercises - Exercise 3 helpful for this question.

A linked list is a linear data structure that is composed of a series of nodes. A node stores a piece of data, and the memory address of the next node.

Conceptually, a linked list that stores the numbers [111, 222, 333] may look like this:

A node whose next pointer points to nullptr indicates that it is at the end of the linked list.

The basic structure of a Node class would look like this:

struct Node {
    int data;
    Node* next;
    
    Node(const int& data, Node* next=nullptr) {
        this->data = data;
        this->next = next;
    }
};

Part 0

Using the above class, create a program that creates a linked list as illustrated above.

Solution:

  • Create three instances of Node with values 111, 222, and 333 respectively.

  • Create a variable called head of type Node*, and assign the memory address of the node with value 111 to this variable.

  • Set the memory address of the node with value 222 to the next member variable of the node with value 111.

  • Set the memory address of the node with the value 333 to the next member variable of the node with value 222.

  • Set the next member variable of the node with value 333 to nullptr.

Part 1

Create a function:

void DisplayLinkedList(Node* head);

That displays the contents of a linked list. For the above example it should output 111 222 333.

Bonus points: can you create three implementations of this function - one using a for loop, and another using a while loop, and the last using recursion?

Part 2

Our linked list only stores its data in the form of ints. Using your knowledge of templates, could you convert the Node class above to be a template class, that can store data as any type?

Node<int> first(111);
Node<int> second(222);
Node<int> third(333);
Node<int>* head;

head = &first;
first.next = &second;
second.next = &third;

DisplayLinkedList(head);
Node<std::string> first("One");
Node<std::string> second("Two");
Node<std::string> third("Three");
Node<std::string>* head;

head = &first;
first.next = &second;
second.next = &third;

DisplayLinkedList(head);

You will have to modify the DisplayLinkedList function to be a template function as well.

Last updated

Was this helpful?