JirR02 fec3fd845f Exercise 11 & Bonus 2
Added Exercise 11 & Bonus 2 to Repo
Added Signature to Exercise 10
2025-05-19 11:34:47 +02:00
..
2025-05-19 11:34:47 +02:00

Task 1a: Understanding Pointers: - Lookup

Task Description

Complete the definition of function lookup by editing the file lookup.cpp according to its specified pre- and post conditions.

We provide a test program using the implemented function to perform lookup on a vector.

Input

The program accepts

  • the length of the vector, l,
  • l vector elements, and
  • a lookup index i.

For example:

means a 3-element vector [9, 8, 7] and the lookup index 1.

Output

The test program prints the value of the element at the lookup index.

For example, for the input given above, this value is 8.

Solution

#include "lookup.h"

// PRE:  0 <= i < vec.size()
// POST: Returns the address of the i-th element of vec.
const int *lookup(const std::vector<int> &vec, const int i) { return &vec[i]; }

Made by JirR02 in Switzerland 🇨🇭