# Chapter 14 - Worked Exercises 3

## Exercise 1 - Utility Template Functions

### Part 1&#x20;

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

```cpp
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&#x20;

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

```cpp
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&#x20;

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

```cpp
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&#x20;

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

The below should illustrate the use of the class:

```cpp
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&#x20;

Can you get the `Calculator` template class to work with the `Rational` class that you made in [Chapter 12 - Worked Exercises 1 - Exercise 1](https://tutoring.cho.io/12/exercises-1#exercise-1-rational-numbers)?

Identify which operators you are missing, and implement them.&#x20;

## Exercise 2 - Generic Vector&#x20;

Refer to [Chapter 12 - Worked Exercises 1 - Exercise 1](https://tutoring.cho.io/12/exercises-1#exercise-1-vector), where you've made a vector class that stores an collection of integers.&#x20;

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

## Exercise 3 - Linked List&#x20;

{% hint style="info" %}
You will find referring to [Chapter 9 - Worked Exercises - Exercise 3](https://tutoring.cho.io/9/exercises#exercise-3-best-friends) helpful for this question.
{% endhint %}

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:

![](https://4122513583-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSrqC8yBLK9rXNyJ6ol%2F-MbhQWU8BeE-MWIrWe8L%2F-MbhTXYuovdJ7mSGAXTM%2FBlank%20diagram%20-%20Page%202.png?alt=media\&token=c1376a85-f100-4f9b-baaa-dd48b6e372d4)

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:

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

### Part 0&#x20;

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.&#x20;
* 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&#x20;

Create a function:

```cpp
void DisplayLinkedList(Node* head);
```

That displays the contents of a linked list. For the above example it should output `111 222 333`.&#x20;

*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**?*&#x20;

### Part 2&#x20;

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?

```cpp
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);
```

```cpp
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.&#x20;
