Exercise 2
Added Exercise 2 to repository
This commit is contained in:
parent
d580b385e5
commit
b9d258de44
14
Informatik_I/Exercise_2/task_1/README.md
Normal file
14
Informatik_I/Exercise_2/task_1/README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Task 1
|
||||||
|
|
||||||
|
This task is a text based task. You do not need to write any program/C++ file: the answer should be written in solutions.txt according to the indicated format. Lines that start with "#" are interpreted as comments. You can run the autograder to check correctness.
|
||||||
|
|
||||||
|
## Task
|
||||||
|
|
||||||
|
Which of the following expressions evaluate to `true`, which to `false`?
|
||||||
|
|
||||||
|
1. `3 >= 3`
|
||||||
|
1. `true || false && false`
|
||||||
|
1. `(true || false) && false`
|
||||||
|
1. `3 > (1 < true)`
|
||||||
|
1. `8 > 4 > 2 > 1`
|
||||||
|
1. `2 < a < 4` (a is a variable of type int)
|
17
Informatik_I/Exercise_2/task_1/solutions.txt
Normal file
17
Informatik_I/Exercise_2/task_1/solutions.txt
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Lines starting with # are comments. Spaces are ignored.
|
||||||
|
|
||||||
|
# Replace the question marks, indicating which of the following expression evaluate to true, which to false.
|
||||||
|
|
||||||
|
|
||||||
|
1. 3 >= 3 == true
|
||||||
|
|
||||||
|
2. true || false && false == true
|
||||||
|
|
||||||
|
3. (true || false) && false == false
|
||||||
|
|
||||||
|
4. 3 > (1 < true) == true
|
||||||
|
|
||||||
|
5. 8 > 4 > 2 > 1 == false
|
||||||
|
|
||||||
|
6. 2 < a < 4 == true
|
||||||
|
|
25
Informatik_I/Exercise_2/task_2/README.md
Normal file
25
Informatik_I/Exercise_2/task_2/README.md
Normal 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`.
|
49
Informatik_I/Exercise_2/task_2/solutions.cpp
Normal file
49
Informatik_I/Exercise_2/task_2/solutions.cpp
Normal 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
|
||||||
|
}
|
||||||
|
}
|
21
Informatik_I/Exercise_2/task_3a/README.md
Normal file
21
Informatik_I/Exercise_2/task_3a/README.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Task
|
||||||
|
|
||||||
|
_Fibonacci numbers_ are the integers in the following sequence: $0, 1, 1, 2, 3, 5, 8, 13, 21, ...$. Each number is the sum of the two previous numbers.
|
||||||
|
|
||||||
|
_Fibonacci primes_ are Fibonacci numbers that are also prime numbers. Write a program that asks the user for an integer $m$ and then computes and prints all Fibonacci primes between $0$ and $m$ (including). Print each number on a new line.
|
||||||
|
|
||||||
|
Finally, on a new line print the total number of Fibonacci primes found.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
If your program is asked to print the Fibonacci primes between $0$ and $14$ the output should look something like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
2
|
||||||
|
3
|
||||||
|
5
|
||||||
|
13
|
||||||
|
Found 4 Fibonacci primes
|
||||||
|
```
|
||||||
|
|
||||||
|
**Important:** using anything other than `int` (e.g., `unsigned int`, floating point numbers, `long`, or double `long`) is forbidden.
|
47
Informatik_I/Exercise_2/task_3a/main.cpp
Normal file
47
Informatik_I/Exercise_2/task_3a/main.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a = 1;
|
||||||
|
int b = 1;
|
||||||
|
int cnt = 0; // Initialization of number of deviders
|
||||||
|
int count = 0; // Initialization of number of Fibonacci primes
|
||||||
|
int n = 2; // Fibonacci number
|
||||||
|
int i = 0; // Input variable
|
||||||
|
|
||||||
|
std::cin >> i;
|
||||||
|
|
||||||
|
for (; n <= i;) {
|
||||||
|
// Reset counters
|
||||||
|
cnt = 0;
|
||||||
|
|
||||||
|
// Check for deviders of Fibonacci number
|
||||||
|
if (n <= 22360) {
|
||||||
|
for (int j = 1; j <= 22360; ++j) { // 22360 = sqrt(500000000)
|
||||||
|
if (n % j == 0) {
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Increase count if Fibonacci number is a prime number
|
||||||
|
if (cnt == 2) {
|
||||||
|
std::cout << n << "\n";
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
} else if (n > 22360) {
|
||||||
|
for (int j = 1; j <= 22360; ++j) { // 22360 = sqrt(500000000)
|
||||||
|
if (n % j == 0) {
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cnt == 1) {
|
||||||
|
std::cout << n << "\n";
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Initialize next Fibonacci number
|
||||||
|
a = b;
|
||||||
|
b = n;
|
||||||
|
n = a + b;
|
||||||
|
}
|
||||||
|
// End message
|
||||||
|
std::cout << count << "\n";
|
||||||
|
}
|
54
Informatik_I/Exercise_2/task_3b/README.md
Normal file
54
Informatik_I/Exercise_2/task_3b/README.md
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# Fibonacci overflow check
|
||||||
|
|
||||||
|
## Background
|
||||||
|
|
||||||
|
_Fibonacci numbers_ are the integers in the following sequence: $0, 1, 1, 2, 3, 5, 8, 13, 21, ...$, where each number is the sum of the two previous numbers.
|
||||||
|
|
||||||
|
## Task
|
||||||
|
|
||||||
|
Fibonacci numbers grow fast, and can thus easily exceed the value range of 32-bit `int`. Think of a general way how you can check if the result of an addition would exceed the range of a 32-bit `int` (i.e. overflow) **without actually performing the addition causing the overflow.**
|
||||||
|
|
||||||
|
Remember that we consider **signed integers.** Because only half of the numbers are positive, this leaves us with 31 bits to store the actual positive number value.
|
||||||
|
|
||||||
|
Write a program that asks the user for an integer $n$ and then prints the first $n$ Fibonacci numbers, each number on a new line. Use an `int` (we assume 32 bits, including the sign) to represent the current Fibonacci number. **Most importantly:** exit the print loop as soon as you detect that an overflow _would occur._
|
||||||
|
|
||||||
|
Finally, again on a new line, output the count $c$ of Fibonacci numbers previously printed, and the initial input $n$ from the user, in the format: `c of n`.
|
||||||
|
|
||||||
|
### Example:
|
||||||
|
|
||||||
|
Let's (wrongly!) assume that $5$ cannot be represented using a 32 bit int. This means that $3$ is the largest 32-bit Fibonacci number. If your program is asked to print the first $4$ Fibonacci numbers the output should look as follows:
|
||||||
|
|
||||||
|
```
|
||||||
|
0
|
||||||
|
1
|
||||||
|
1
|
||||||
|
2
|
||||||
|
Printed 4 of 4 Fibonacci numbers
|
||||||
|
```
|
||||||
|
|
||||||
|
If you instead ask it to print the first 100 Fibonacci numbers the output should look as follows:
|
||||||
|
|
||||||
|
```
|
||||||
|
0
|
||||||
|
1
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
Printed 5 of 100 Fibonacci numbers
|
||||||
|
```
|
||||||
|
|
||||||
|
**Important:** using anything other than `int` (e.g., `unsigned int`, floating point numbers, `long`, or double `long`) is forbidden.
|
||||||
|
|
||||||
|
**Restrictions:**
|
||||||
|
|
||||||
|
- The program **must not** rely on the knowledge of its final result. In particular, it is not allowed to hard-code
|
||||||
|
- the largest 32-bits Fibonacci number, or
|
||||||
|
- the number of digits that it has, or
|
||||||
|
- the total number of Fibonacci numbers representable with a 32-bit int
|
||||||
|
- Most importantly: do not perform additions that cause an overflow on 32 bit
|
||||||
|
|
||||||
|
**Note:** It is straightfoward to compute the largest (signed) integer representable with 32 bits. You are also explicitly allowed to hard-code this value in your program.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Warning:** The autograder does not catch if an addition causes an overflow or if you do anything that's disallowed in the "Restrictions" section above, but you will receive 0 points when your TA corrects and catches this.
|
26
Informatik_I/Exercise_2/task_3b/main.cpp
Normal file
26
Informatik_I/Exercise_2/task_3b/main.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int a = 0; // First Fibonacci number
|
||||||
|
int b = 1; // Second Fibonacci number
|
||||||
|
int j = 0; // New Fibonacci number
|
||||||
|
int c = 0; // Number of Fibonacci numbers
|
||||||
|
int max = 2147483647;
|
||||||
|
int n; // Input integer
|
||||||
|
|
||||||
|
std::cin >> n;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
|
||||||
|
if (max - a < b) { // Check if the new Fibonacci number goes into overflow
|
||||||
|
break;
|
||||||
|
} else { // otherwise, calculate next Fibonacci number
|
||||||
|
std::cout << j << "\n";
|
||||||
|
a = b;
|
||||||
|
b = j;
|
||||||
|
j = a + b;
|
||||||
|
++c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << c << " of " << n; // End Message
|
||||||
|
}
|
14
Informatik_I/Exercise_2/task_4/README.md
Normal file
14
Informatik_I/Exercise_2/task_4/README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
## Task
|
||||||
|
|
||||||
|
Write a program that inputs a non-negative integer `n` (but store it as `int`) and outputs the binary digits of `n` in the _correct_ order (i.e., starting with the most significant bit). Do not output the leading zeros or the sign.
|
||||||
|
|
||||||
|
**Hint:** In order to find the largest integer $k$ such that $2^k \leq x$, you can utilize that $k$ is the smallest integer such that $2^k > \frac{x}{2}$. This observation is particularly useful to avoid an overflow for the expression $2^k$ when searching for the most significant bit to represent $x$.
|
||||||
|
|
||||||
|
**Restrictions:**
|
||||||
|
|
||||||
|
- Libraries: only the iostream standard library header is allowed; using arrays, string or cmath is not permitted.
|
||||||
|
- Operators: you may not use bitshift operators to manipulate the numbers.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Warning:** The autograder does not catch if you do anything that's disallowed in the "Restrictions" section above, but you will receive 0 points when your TA corrects and catches this.
|
32
Informatik_I/Exercise_2/task_4/main.cpp
Normal file
32
Informatik_I/Exercise_2/task_4/main.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int i = 0; // Input integer
|
||||||
|
int d = 0; // Input Number
|
||||||
|
int a = 0; // Part of binary number
|
||||||
|
int b = 0; // Number to divide
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
std::cin >> i;
|
||||||
|
d = i;
|
||||||
|
b = i;
|
||||||
|
|
||||||
|
if (i < 0) {
|
||||||
|
return 0;
|
||||||
|
} else if (i == 0) {
|
||||||
|
std::cout << 0;
|
||||||
|
} else {
|
||||||
|
while (d != 0) {
|
||||||
|
d = d / 2;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
for (; count > 0; --count) {
|
||||||
|
for (int j = count; j > 1; --j) {
|
||||||
|
b = b / 2;
|
||||||
|
}
|
||||||
|
a = b % 2;
|
||||||
|
std::cout << a;
|
||||||
|
b = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user