Converted everything to orgmode

converted everything to orgmode and added solution to the README files
This commit is contained in:
2025-03-31 08:37:19 +02:00
parent 3013d7ad47
commit 311cb8434c
25 changed files with 319 additions and 502 deletions

View File

@@ -13,11 +13,11 @@ For example, in the following game player **A** won:
* Your task
The file `game.cpp` contains a partial implementation of the game. The main game loop is implemented in the function `play_game(grid)`. This function is called from `main.cpp` and gets the game grid as an argument. The game grid is already fully implemented. We describe the functionality it provides in a following section below.
The file =game.cpp= contains a partial implementation of the game. The main game loop is implemented in the function =play_game(grid)=. This function is called from =main.cpp= and gets the game grid as an argument. The game grid is already fully implemented. We describe the functionality it provides in a following section below.
Your task is extending the game implementation in file `game.cpp` such that it works the way described above. We provide some functions in this file and thus suggest a certain structure -- but you are completely free to do it your way. Add, remove or change anything you want. The only constraint is that you must implement the function play_game which takes the game grid as argument. You are of course free to change the body of this function in whatever way you like.
Your task is extending the game implementation in file =game.cpp= such that it works the way described above. We provide some functions in this file and thus suggest a certain structure -- but you are completely free to do it your way. Add, remove or change anything you want. The only constraint is that you must implement the function play_game which takes the game grid as argument. You are of course free to change the body of this function in whatever way you like.
If you do decide to change the body of the play_game function be careful to not accidentally change the format of the outputs it generates. For example: If you change the output `std::cout << "Game finished" << std::endl;` to `std::cout << "Game Finished" << std::endl;` you will not pass some test cases. The autograder is very picky when checking whether or not your outputs are correct. You can get all the points in this exercise without changing any of the output lines in the template.
If you do decide to change the body of the play_game function be careful to not accidentally change the format of the outputs it generates. For example: If you change the output =std::cout << "Game finished" << std::endl;= to =std::cout << "Game Finished" << std::endl;= you will not pass some test cases. The autograder is very picky when checking whether or not your outputs are correct. You can get all the points in this exercise without changing any of the output lines in the template.
/Note/: More Information on the the concept of the game on Code Expert.

View File

@@ -3,7 +3,7 @@
* Task
Let `a`, `b`, `c`, and `d` be variables of type `int`.
Let =a=, =b=, =c=, and =d= be variables of type =int=.
- Which of the following character sequences are valid expressions in the sense that they are accepted by a C++ Compiler? Explain your answer.
@@ -15,7 +15,7 @@ Let `a`, `b`, `c`, and `d` be variables of type `int`.
Assume that all the variables have been defined and correctly initialized and that all expressions are completely independent from each other.
- For each of the expressions that you have identified as valid, decide whether the entire expression is an `lvalue` or an `rvalue`, and explain your decision.
- For each of the expressions that you have identified as valid, decide whether the entire expression is an =lvalue= or an =rvalue=, and explain your decision.
- Determine the values of the expressions that you have identified as valid and explain how these values are obtained.

View File

@@ -3,11 +3,11 @@
* Task
/This task is a text-based task but mostly automatically checked. You are required to write your answers into `submission.txt` by replacing the question marks with the correct solution. Please, do not change the line formating./
/This task is a text-based task but mostly automatically checked. You are required to write your answers into =submission.txt= by replacing the question marks with the correct solution. Please, do not change the line formating./
/You can check whether your solution is correct by clicking on the test button./
Numbers can be provided in various formats in C++. Literals prefixed with `0b` indicate binary encoding. Assume unsigned arithmetics with sufficient numbers of bits, i.e. no overflows. Convert the following binary numbers into decimal numbers (1-4) and decimal numbers to binary (5-8):
Numbers can be provided in various formats in C++. Literals prefixed with =0b= indicate binary encoding. Assume unsigned arithmetics with sufficient numbers of bits, i.e. no overflows. Convert the following binary numbers into decimal numbers (1-4) and decimal numbers to binary (5-8):
#+begin_src txt
# Lines starting with # are comments. Spaces is ignored.

View File

@@ -16,9 +16,9 @@ We assume that \(R_1\), \(R_2\), \(R_3\), and \(R_4\) have an integer valued res
You can find formulas for computing the total resistance in this [[https://en.wikipedia.org/wiki/Resistor#Series_and_parallel_resistors][Wikipedia article]].
*Important:* using anything other than `int` (e.g., floating point numbers, `long`, or `double long`) is forbidden.
*Important:* using anything other than =int= (e.g., floating point numbers, =long=, or =double long=) is forbidden.
*Important:* using `if-else` and any other branches is forbidden.
*Important:* using =if-else= and any other branches is forbidden.
* Solution

View File

@@ -6,14 +6,14 @@ This task is a text based task. You do not need to write any program/C++ file: t
* Task
Which of the following expressions evaluate to `true`, which to `false`?
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)
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)
* Solutions

View File

@@ -3,9 +3,9 @@
* 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`).
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./
/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:*
@@ -13,7 +13,7 @@ Translate the following natural language expressions to `C++` expressions. Assum
return a > 3 && a < 5;
#+end_src
/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.
/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\).
@@ -23,9 +23,9 @@ return a > 3 && a < 5;
** 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`.
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`.
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
@@ -80,3 +80,7 @@ bool task5(bool a, int b) {
}
}
#+end_src
-----
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -20,7 +20,7 @@ If your program is asked to print the Fibonacci primes between $0$ and $14$ the
Found 4 Fibonacci primes
#+end_src
*Important:* using anything other than `int` (e.g., `unsigned int`, floating point numbers, `long`, or double `long`) is forbidden.
*Important:* using anything other than =int= (e.g., =unsigned int=, floating point numbers, =long=, or double =long=) is forbidden.
* Solutions

View File

@@ -7,17 +7,17 @@
* 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.*
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./
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`.
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:
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:
#+begin_src shell
0
@@ -38,7 +38,7 @@ If you instead ask it to print the first 100 Fibonacci numbers the output should
Printed 5 of 100 Fibonacci numbers
#+end_src
*Important:* using anything other than `int` (e.g., `unsigned int`, floating point numbers, `long`, or double `long`) is forbidden.
*Important:* using anything other than =int= (e.g., =unsigned int=, floating point numbers, =long=, or double =long=) is forbidden.
*Restrictions:*
@@ -56,7 +56,7 @@ Printed 5 of 100 Fibonacci numbers
* Mistakes
- The variable `j` goes into overflow which is not allowed!
- The variable =j= goes into overflow which is not allowed!
* Solution

View File

@@ -3,7 +3,7 @@
* 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.
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\).

View File

@@ -86,3 +86,7 @@ void loop() {
std::cout << x << std::endl;
}
#+end_src
-----
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -19,7 +19,7 @@ A natural number \(m\) (\(m \geq 1\)).
The approximation of $\pi$ given by $m$ terms, rounded to 6 significant digits. Note that 6 significant digits is the default precision of C++ for printing floating-point values. Use a double variable to store the sum.
**Important**: the use of functions from the math library (e.g., `pow`) is prohibited.
**Important**: the use of functions from the math library (e.g., =pow=) is prohibited.
* Solution

View File

@@ -5,13 +5,13 @@
Consider a parabola \((\m{P})\) defined as \(y = g(x)\), with \(g(x) = 0.9 \cdot x^2 + 1.3 \cdot x - 0.7\).
Write a program that determines if a point \((x,y)\) lies on parabola \((\m{P})\) or not. The input is provided by two decimal numbers in the sequence \(x,y\). The program must output `yes`, if the point lies on the parabola, otherwise `no`. Use datatype `double` for all variables and numbers used in the calculation of \(g(x)\).
Write a program that determines if a point \((x,y)\) lies on parabola \((\m{P})\) or not. The input is provided by two decimal numbers in the sequence \(x,y\). The program must output =yes=, if the point lies on the parabola, otherwise =no=. Use datatype =double= for all variables and numbers used in the calculation of \(g(x)\).
You will notice that a straight forward approach (comparing for equality) does not work, i.e., for some points that clearly should be on parabola $g$ such an approach returns result `no`.
You will notice that a straight forward approach (comparing for equality) does not work, i.e., for some points that clearly should be on parabola $g$ such an approach returns result =no=.
/Hint/: Look at the difference between the exact values of the function and the values that your program calculates. Change the program so that it works properly for all points the submission system uses as test input without hard-coding the points. Expect an epsilon within the range \([ 10^{-6}, 10^{-3}]\)$. Experiment yourself to find the epsilon required to pass the test cases.
**Note**: Output only with `std::cout << "no" << std::endl;` or `std::cout << "yes" << std::endl;`, as the autograder will only accept output that exactly matches `yes\n` or `no\n`. For all other messages, use `std::cerr` as in:
**Note**: Output only with =std::cout << "no" << std::endl;= or =std::cout << "yes" << std::endl;=, as the autograder will only accept output that exactly matches =yes\n= or =no\n=. For all other messages, use =std::cerr= as in:
#+begin_src cpp
std::cerr << "This is a test message\n"

View File

@@ -3,7 +3,7 @@
* Task
Implement the following rounding function that rounds a 64-bit floating point number (type `double`) to the nearest 32-bit integer (type `int`). You may assume that the type `double` complies with the IEEE standard 754. The function is only required to work correctly if the nearest integer is in the value range of the type `int`, otherwise, the return value of the function is undefined.
Implement the following rounding function that rounds a 64-bit floating point number (type =double=) to the nearest 32-bit integer (type =int=). You may assume that the type =double= complies with the IEEE standard 754. The function is only required to work correctly if the nearest integer is in the value range of the type =int=, otherwise, the return value of the function is undefined.
**Note: Usage of library rounding functions (standard or others) is not allowed.**
@@ -14,9 +14,9 @@ Implement the following rounding function that rounds a 64-bit floating point nu
int round_number(double x);
#+end_src
Write your solution in `rounding.h`.
Write your solution in =rounding.h=.
/Hint/: In `C++`, when you convert a `float` or `double` to an `int`, the fractional part gets cut off (truncated). Think about how you can use the truncated number to solve the exercise.
/Hint/: In =C++=, when you convert a =float= or =double= to an =int=, the fractional part gets cut off (truncated). Think about how you can use the truncated number to solve the exercise.
* Solution

View File

@@ -40,9 +40,9 @@ void print_weekday(int day, int month, int year);
To complete the task, you have to provide the definition of the aforementioned functions.
The `calendar.cpp` file contains skeletons of the functions to be implemented. The `main.cpp` file contains testing functions and the main function. In the main function, a menu is printed to select the function that has to be tested.
The =calendar.cpp= file contains skeletons of the functions to be implemented. The =main.cpp= file contains testing functions and the main function. In the main function, a menu is printed to select the function that has to be tested.
**Important** the main is not editable. Required functions must be implemented in `calendar.cpp`.
**Important** the main is not editable. Required functions must be implemented in =calendar.cpp=.
**Additional notes:**

View File

@@ -2,7 +2,7 @@
/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.
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.

View File

@@ -7,12 +7,12 @@ Run-length encoding is a simple data compression technique that represents \(N\)
[[./pictures/encode_decode.png]]
Write a program that implements run-length encoding and decoding of a byte sequence as described above. By a byte, we mean an integer value in the range \([ 0; 255 ]\). Use the stepwise refinement method to implement the program. **Your solution must consist of at least two functions, encode and decode. Please implement them in `run_length.cpp`.**
Write a program that implements run-length encoding and decoding of a byte sequence as described above. By a byte, we mean an integer value in the range \([ 0; 255 ]\). Use the stepwise refinement method to implement the program. **Your solution must consist of at least two functions, encode and decode. Please implement them in =run_length.cpp=.**
The _input_ is structured as follows:
1. One integer that determines whether to encode: \(0\) or decode: \(1\).
1. Byte sequence to be encoded or decoded (of arbitrary length). If a value outside the range \([ 0; 255 ]\) (except $-1$) is entered, output `error` and stop the en- or decoding.
1. Byte sequence to be encoded or decoded (of arbitrary length). If a value outside the range \([ 0; 255 ]\) (except $-1$) is entered, output =error= and stop the en- or decoding.
1. Integer -1 signaling the end of the byte sequence. Any extra input should be ignored.
For the example above, the inputs are:
@@ -39,13 +39,13 @@ I.e., you can 'reuse' the output as the input.
**Note 1):** As the encoded sequence must be a _byte_ sequence, runs of length 256 or longer need to be split into multiple runs of length 255 at most.
**Note 2):** The first input element (the integer that determines wether to encode or decode), is already consumed by the `main`, that calls either the encode or decode function.
**Note 2):** The first input element (the integer that determines wether to encode or decode), is already consumed by the =main=, that calls either the encode or decode function.
**Note 3):** Your output should not be followed by new line (i.e., do not use `std::endl` or `\n` at the end of your printout)
**Note 3):** Your output should not be followed by new line (i.e., do not use =std::endl= or =\n= at the end of your printout)
**Note 4):** The program will print your output (the result of the decoding or encoding), surrounded by asterisks. You don't have to worry about them, the autograder can safely recognize your solution
**Note 5):** Output only what is strictly required (the encoded or decoded sequence). The autograder will only accept output that exactly matches the expected result. For all other messages, use `std::cerr` as in:
**Note 5):** Output only what is strictly required (the encoded or decoded sequence). The autograder will only accept output that exactly matches the expected result. For all other messages, use =std::cerr= as in:
#+begin_src cpp
std::cerr << "This is a test message\n"
@@ -55,10 +55,10 @@ Those will be ignored by the autograder.
**Special cases**: While decoding a byte sequence two special cases can occur. These must be handled as follows:
1. If a byte sequence ends in the middle of a tuple, stop printing the output of en- or decoding and output `error`.
1. If a byte sequence ends in the middle of a tuple, stop printing the output of en- or decoding and output =error=.
1. Tuples of run-length 0 are possible. For such tuples, output only the leading indicator (\(0\)/\(1\)) and the trailing \(-1\).
**Hint**: You can enter multiple numbers at once separated with a space on the console, e.g, you can copy and paste the above examples, and sequentially read them using multiple `std::cin >> _var_` calls.
**Hint**: You can enter multiple numbers at once separated with a space on the console, e.g, you can copy and paste the above examples, and sequentially read them using multiple =std::cin >> _var_= calls.
Also note that, even though the program's output and the user input are both shown in the same console, they are processed separately internally, so you do not have to worry that the two will mix: if your program outputs something to the console before reading another value, it will only read the user input and not the previous output value. See the Calculator code in the Lecture 4 handout for an example of reading input and producing output in a loop.