Exercise 2

Added Exercise 2 to repository
This commit is contained in:
2025-03-01 13:38:33 +01:00
parent d580b385e5
commit b9d258de44
10 changed files with 299 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
# Task
Translate the following natural language expressions to `C++` expressions. Assume that all the variables are non-negative integers or boolean (of value `true` or `false`).
_For this task you need to write the solutions in the `solutions.cpp` file, by filling the various functions that have been defined for each subtasks._
**Example:** $a$ is greater than $3$ and smaller than $5$. $\Longrightarrow$ Solution:
```cpp
return a > 3 && a < 5;
```
_Note:_ Do not confuse the bitwise logical operators (e.g., `&`) with their binary logical counterparts (`&&`). The semantics are slightly different — bitwise operators do not exhibit short circuit evaluation.
1. $a$ greater than $b$ and the difference between $a$ and $b$ is smaller than $15$.
1. $a$ is an even natural number greater than $a$.
1. $a$ is at most $5$ times greater than $b$ (but can also be smaller than $b$) and at least $5$ times greater than $c$.
1. Either $a$ and $b$ are both false or $c$ is true, but not both.
1. $a$ is false and $b$ is zero.
## Input
The program expects the task number as the first input followed by the parameters to the chosen task. For example, `3 5 1 1` selects task `3` with `a = 5`, `b = 1`, and `c = 1`.
Note that boolean parameters for tasks 4 and 5 are entered as true and false. For example `4 true false true` would run task `4` with `a = true`, `b = false`, and `c = true`.

View File

@@ -0,0 +1,49 @@
#include "solutions.h"
// Fill out each function with the appropriate expression
bool task1(int a, int b) {
// a greater than b and the difference between a and b is smaller than 15.
if (a > b && (a - b) < 15) {
return true;
} else {
return false;
}
}
bool task2(int a) {
// a is an even natural number greater than 3.
if (a > 3 && a % 2 == 0) {
return true;
} else {
return false;
}
}
bool task3(int a, int b, int c) {
// a is at most 5 times greater than b (but can also be smaller than b)
// and at least 5 times greater than c.
if (a <= 5 * b && a >= 5 * c) {
return true;
} else {
return false;
}
}
bool task4(bool a, bool b, bool c) {
// Either a and b are both false or c is true, but not both.
if ((a == false && b == false) != (c == true)) {
return true; // Replace with your solution
} else {
return false;
}
}
bool task5(bool a, int b) {
// a is false and b is zero.
if (a == false && b == false) {
return true;
} else {
return false; // Replace with your solution
}
}