JirR02 88e0b5ed69 Converted everything to orgmode
converted everything to orgmode and added solution to the README files
2025-03-31 08:40:43 +02:00
..
2025-03-31 08:40:43 +02:00

Task 2: Pre- and post-conditions

This task is a text-based task. You do not need to write any program/C++ file: the answer should be written in functions.cpp.

Consider the functions implemented in functions.cpp. For each function, add proper pre- and post-conditions.

  • If no pre-condition is needed, you can simply write "n/a".
  • The post-condition does not have to be a mathematical formula, e.g. it can be an informal description, but it must completely characterize the results and effects of the functions depending on the provided parameters.

Note: For the purposes of this task, you can ignore overflows.

Solution

// PRE:  x > 0
// POST: returns n^x
int f1(int n, int x) {
  int res = 1;
  for (; x > 0; x--) {
    res *= n;
  }
  return res;
}

// PRE:  n > 0
// POST: returns number of devisions by 10 with n
int f2(int n) {
  int i = 0;
  while (n > 0) {
    n = n / 10;
    ++i;
  }
  return i;
}

// PRE:  n > 1
// POST: returns a bool to determine if n is prime
bool f3(int n) {
  int i = 2;
  for (; n % i != 0; ++i);
  return n == i;
}

// PRE:  n is square number
// POST: returns root of n
int f4(int n) {
  int i = 0;
  while (i * i != n) {
    ++i;
  }
  return i;
}

Made by JirR02 in Switzerland 🇨🇭