Task 2: From Natural Language to C++
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:
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.
- \(a\) greater than \(b\) and the difference between \(a\) and \(b\) is smaller than \(15\).
- \(a\) is an even natural number greater than \(a\).
- \(a\) is at most \(5\) times greater than \(b\) (but can also be smaller than \(b\)) and at least \(5\) times greater than \(c\).
- Either \(a\) and \(b\) are both false or \(c\) is true, but not both.
- \(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
.
Solutions
#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
}
}
Made by JirR02 in Switzerland 🇨🇭