Converted everything to orgmode

converted everything to orgmode and added solution to the README files
This commit is contained in:
2025-03-31 08:40:43 +02:00
parent 8719f4c140
commit 88e0b5ed69
88 changed files with 1942 additions and 2989 deletions

View File

@@ -1,24 +0,0 @@
_This task is a mixed text/programming task. You need to update the content of loop.cpp according to the instruction provided in the file. For the code part, please check the autograder output._
# Task
Considering the snippet
```cpp
int n;
std::cin >> n;
int f = 1;
if (n > 0) {
do {
f = f * n;
--n;
} while (n > 0);
}
std::cout << f << std::endl;
```
1. Describe what it computes.
1. Decide which of the other two kind of loops would fit better than the one it is currently using, and describe why.
1. Rewrite the snippet into the loop you specified in (2). This part is autograded.
**Important**: The use of goto statements is prohibited.

View File

@@ -1,3 +1,34 @@
#+TITLE: Task 1: Loop mix-up: Snippet 1
#+AUTHOR: JirR02
/This task is a mixed text/programming task. You need to update the content of loop.cpp according to the instruction provided in the file. For the code part, please check the autograder output./
* Task
Considering the snippet
#+begin_src cpp
int n;
std::cin >> n;
int f = 1;
if (n > 0) {
do {
f = f * n;
--n;
} while (n > 0);
}
std::cout << f << std::endl;
#+end_src
1. Describe what it computes.
1. Decide which of the other two kind of loops would fit better than the one it is currently using, and describe why.
1. Rewrite the snippet into the loop you specified in (2). This part is autograded.
*Important*: The use of goto statements is prohibited.
* Solution
#+begin_src cpp
#include "loop.h"
#include <iostream>
@@ -41,3 +72,8 @@ void loop() {
std::cout << f << std::endl;
}
#+end_src
-—---
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -1,23 +0,0 @@
_This task is a mixed text/programming task. You need to update the content of loop.cpp according to the instruction provided in the file. For the code part, please check the autograder output._
# Task
Considering the snippet
```cpp
for (;;) {
int i1, i2;
std::cin >> i1 >> i2;
std::cout << i1 + i2 << "\n";
int again;
std::cout << "Again? (0/1)\n";
std::cin >> again;
if (again == 0) break;
}
```
1. Describe what it computes.
1. Decide which of the other two kind of loops would fit better than the one it is currently using, and describe why.
1. Rewrite the snippet into the loop you specified in (2). This part is autograded. Note: print the control message "Again? (0/1)" using the same format used in the snippet.

View File

@@ -1,3 +1,33 @@
#+TITLE: Task 2: Loop mix-up: Snippet 2
#+AUTHOR: JirR02
/This task is a mixed text/programming task. You need to update the content of loop.cpp according to the instruction provided in the file. For the code part, please check the autograder output./
* Task
Considering the snippet
#+begin_src cpp
for (;;) {
int i1, i2;
std::cin >> i1 >> i2;
std::cout << i1 + i2 << "\n";
int again;
std::cout << "Again? (0/1)\n";
std::cin >> again;
if (again == 0) break;
}
#+end_src
1. Describe what it computes.
1. Decide which of the other two kind of loops would fit better than the one it is currently using, and describe why.
1. Rewrite the snippet into the loop you specified in (2). This part is autograded. Note: print the control message "Again? (0/1)" using the same format used in the snippet.
* Solution
#+begin_src cpp
#include "loop.h"
#include <iostream>
@@ -46,3 +76,8 @@ void loop() {
std::cin >> again;
}
}
#+end_src
-----
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -1,27 +0,0 @@
_This task is a mixed text/programming task. You need to update the content of loop.cpp according to the instructions provided in the file. For the code part, please check the autograder output._
# Task
Consider the following program:
```cpp
int n;
std::cin >> n;
int x = 1;
if (n > 0) {
bool e = true;
do {
if (--n == 0) {
e = false;
}
x *= 2;
} while (e);
}
std::cout << x << std::endl;
```
1. Describe the output of the program as a function of its input n.
1. For which values of `n` do you expect a correct output `x`? Explain why.
1. Show that this program terminates for all values of `n` found in (2). Hint: Make an argument based on the following idea. First prove that the program terminates for the `n=0` and `n=1`. Then show that it terminates for any other `n` knowing that it terminates for `n-1`. This creates a domino effect where the fact that the program terminates for `n=1` proves that the program must also terminate for `n=2`, and this in turn proves that the program must terminate for `n=3`, and so on. This technique is called "proof by induction".
1. Provide a more elegant implementation of this function using another type of loop. This part is autograded.

View File

@@ -0,0 +1,92 @@
#+TITLE: Task 3: Loop Analysis
/This task is a mixed text/programming task. You need to update the content of loop.cpp according to the instructions provided in the file. For the code part, please check the autograder output./
* Task
Consider the following program:
#+begin_src cpp
int n;
std::cin >> n;
int x = 1;
if (n > 0) {
bool e = true;
do {
if (--n == 0) {
e = false;
}
x *= 2;
} while (e);
}
std::cout << x << std::endl;
#+end_src
1. Describe the output of the program as a function of its input \(n\).
1. For which values of \(n\) do you expect a correct output \(x\)? Explain why.
1. Show that this program terminates for all values of \(n\) found in (2). Hint: Make an argument based on the following idea. First prove that the program terminates for the \(n=0\) and \(n=1\). Then show that it terminates for any other \(n\) knowing that it terminates for \(n-1\). This creates a domino effect where the fact that the program terminates for \(n=1\) proves that the program must also terminate for \(n=2\), and this in turn proves that the program must terminate for \(n=3\), and so on. This technique is called "proof by induction".
1. Provide a more elegant implementation of this function using another type of loop. This part is autograded.
* Solution
#+begin_src cpp
#include "loop.h"
#include <iostream>
// Fill out the file with the required answers
/*
Subtask 1: Describe the output of the program as a function of its input n.
This code snippte computes the powers of 2 with a positive exponential. The
exponential is the input by the user.
*/
/*
Subtask 2: For which values of n do you expect a correct output x? Explain
why.
For a exponential between 0 and 30. Anything below zero would result in the
wrong answer (1) and anything above 30 would result in an overflow. In
addition, if we imporved the code to calculate the powers of 2 with negative
exponents, it would not work since the result of the powers of 2 with negative
exponents are variables of type float or double and the code snippet is
working with integers.
*/
/*
Subtask 3: Show that this program terminates for all values of n found in (2).
If the input is 31, the output is -2147483648.
If the input is -1, the output is 1.
*/
/*
Subtask 4: Provide a more elegant implementation of this function using
another type of loop.
*/
void loop() {
int n;
std::cin >> n;
int x = 1;
for (; n > 0; --n)
x *= 2;
std::cout << x << std::endl;
}
#+end_src
-----
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -1,54 +0,0 @@
#include "loop.h"
#include <iostream>
// Fill out the file with the required answers
/*
Subtask 1: Describe the output of the program as a function of its input n.
This code snippte computes the powers of 2 with a positive exponential. The
exponential is the input by the user.
*/
/*
Subtask 2: For which values of n do you expect a correct output x? Explain
why.
For a exponential between 0 and 30. Anything below zero would result in the
wrong answer (1) and anything above 30 would result in an overflow. In
addition, if we imporved the code to calculate the powers of 2 with negative
exponents, it would not work since the result of the powers of 2 with negative
exponents are variables of type float or double and the code snippet is
working with integers.
*/
/*
Subtask 3: Show that this program terminates for all values of n found in (2).
If the input is 31, the output is -2147483648.
If the input is -1, the output is 1.
*/
/*
Subtask 4: Provide a more elegant implementation of this function using
another type of loop.
*/
void loop() {
int n;
std::cin >> n;
int x = 1;
for (; n > 0; --n)
x *= 2;
std::cout << x << std::endl;
}

View File

@@ -1,19 +0,0 @@
# Task
The number $\pi$ can be defined through various infinite sums. The accuracy increases with the number of terms. Considering the following sum, that we call sum 1:
$$\frac{\pi}{4} = \sum_{j=0}^{m-1} \frac{(-1)^j}{2j + 1} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + ...$$
Note that $m$ is the number of the terms in the sum. For example, $m=2$ refers to the sum of the terms $1$ (for $j=0$) and $-\frac{1}{3}$ (for $j=1$). This examples yields a value of $4 \cdot (1-\frac{1}{3})$ for $\pi$.
Write a program that computes and outputs an approximation of Pi, based on sum 1. The input for your program is the number of **terms** $m$ of sum 1 that should be considered in the calculation. The output is the approximation of $\pi$.
## Input
A number $m \geq 1$.
## Output
The approximation of $\pi$ given by $4 \sum_{j=0}^{m-1} \frac{(-1)^j}{2j + 1}$, rounded to 6 significant digits. Note that 6 significant digits is the default precision of C++ for printing floating-point values. Use a variable of type double to calculate the sum. Note that that $x^0$ is 1 (if $x \neq 0$).
**Important**: the use of functions from the math library (e.g., pow) is prohibited.

View File

@@ -0,0 +1,61 @@
#+TITLE: Task 4a: Approximation of Pi: Sum 1
#+AUTHOR: JirR02
* Task
The number \(\pi\) can be defined through various infinite sums. The accuracy increases with the number of terms. Considering the following sum, that we call sum 1:
$$\frac{\pi}{4} = \sum_{j=0}^{m-1} \frac{(-1)^j}{2j + 1} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + ...$$
Note that \(m\) is the number of the terms in the sum. For example, \(m=2\)$ refers to the sum of the terms \(1\) (for \(j=0\)) and \(-\frac{1}{3}\) (for \(j=1\)). This examples yields a value of \(4 \cdot (1-\frac{1}{3})\) for \(\pi\).
Write a program that computes and outputs an approximation of Pi, based on sum 1. The input for your program is the number of **terms** $m$ of sum 1 that should be considered in the calculation. The output is the approximation of \(\pi\).
** Input
A number \(m \geq 1\).
** Output
The approximation of \(\pi\) given by \(4 \sum_{j=0}^{m-1} \frac{(-1)^j}{2j + 1}\), rounded to 6 significant digits. Note that 6 significant digits is the default precision of C++ for printing floating-point values. Use a variable of type double to calculate the sum. Note that \(x^0\) is 1 (if \(x \neq 0\)).
**Important**: the use of functions from the math library (e.g., pow) is prohibited.
* Solution
#+begin_src cpp
#include <iostream>
double power(double n, double e) { // Self defined function for exponent
int res = 1;
for (; e > 0; --e) {
res *= n;
}
return res;
}
int main() {
double m; // User Input
double pi = 0; // Pi
float res; // Outputed result with 6 significant digits
std::cin >> m;
if (m < 1) { // Check if Input is greater than 1.
return 0;
} else {
for (int i = 0; i < m; ++i) {
pi = pi + 4 * ((power(-1, i)) / ((2 * i) + 1)); // calculate Pi
}
res = (float)(pi); // round to 6 significant digits
std::cout << res;
return 0;
}
}
#+end_src
-----
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -1,30 +0,0 @@
#include <iostream>
double power(double n, double e) { // Self defined function for exponent
int res = 1;
for (; e > 0; --e) {
res *= n;
}
return res;
}
int main() {
double m; // User Input
double pi = 0; // Pi
float res; // Outputed result with 6 significant digits
std::cin >> m;
if (m < 1) { // Check if Input is greater than 1.
return 0;
} else {
for (int i = 0; i < m; ++i) {
pi = pi + 4 * ((power(-1, i)) / ((2 * i) + 1)); // calculate Pi
}
res = (float)(pi); // round to 6 significant digits
std::cout << res;
return 0;
}
}

View File

@@ -1,20 +0,0 @@
# Task
The number $\pi$ can be defined through various infinite sums. The accuracy increases with the number of terms. Considering the following sum, which we call sum 2:
$$\frac{\pi}{2} = 1 + \sum_{j = 1}^{m - 1} \frac{\prod_{i=1}^j i}{\prod_{i=1}^j (2i + 1)} = 1 + \frac{1}{3} + \frac{1 \cdot 2}{3 \cdot 5} + \frac{1 \cdot 2 \cdot 3}{3 \cdot 5 \cdot 7} + ...$$
Write a program that computes and outputs an approximation of Pi, based on sum 2. The input for your program is the number of **terms** $m$ (including 1) to be included in the calculation to be done. The output is the approximation of $\pi$.
**Optional**: After you have solved this and the previous task, think about which formula gives a more precise approximation of
, sum 1 or sum 2 ? What are the drawbacks? Write your thoughts in a comment below the code.
## Input
A natural number $m$ ($m \geq 1$).
## Output
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.

View File

@@ -0,0 +1,66 @@
#+TITLE: Task 4b: Approximation of Pi: Sum 2
#+AUTHOR: JirR02
* Task
The number \(\pi\) can be defined through various infinite sums. The accuracy increases with the number of terms. Considering the following sum, which we call sum 2:
$$\frac{\pi}{2} = 1 + \sum_{j = 1}^{m - 1} \frac{\prod_{i=1}^j i}{\prod_{i=1}^j (2i + 1)} = 1 + \frac{1}{3} + \frac{1 \cdot 2}{3 \cdot 5} + \frac{1 \cdot 2 \cdot 3}{3 \cdot 5 \cdot 7} + ...$$
Write a program that computes and outputs an approximation of Pi, based on sum 2. The input for your program is the number of **terms** $m$ (including 1) to be included in the calculation to be done. The output is the approximation of \(\pi\).
**Optional**: After you have solved this and the previous task, think about which formula gives a more precise approximation of \(\pi\), sum 1 or sum 2 ? What are the drawbacks? Write your thoughts in a comment below the code.
* Input
A natural number \(m\) (\(m \geq 1\)).
* Output
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.
* Solution
#+begin_src cpp
#include <iostream>
double productsumnum(double n) {
double res = 1;
for (int j = 1; j <= n; ++j) {
res *= j;
}
return res;
}
double productsumden(double n) {
double res = 1;
for (int j = 1; j <= n; ++j) {
res *= (2 * j + 1);
}
return res;
}
int main() {
double m; // User Input
double pi = 2; // Pi
float res; // Outputed result with 6 significant digits
std::cin >> m;
for (int i = 1; i < m; ++i) {
pi += (2 * (productsumnum(i) / productsumden(i)));
}
res = (float)(pi);
std::cout << res;
return 0;
}
#+end_src
-----
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -1,35 +0,0 @@
#include <iostream>
double productsumnum(double n) {
double res = 1;
for (int j = 1; j <= n; ++j) {
res *= j;
}
return res;
}
double productsumden(double n) {
double res = 1;
for (int j = 1; j <= n; ++j) {
res *= (2 * j + 1);
}
return res;
}
int main() {
double m; // User Input
double pi = 2; // Pi
float res; // Outputed result with 6 significant digits
std::cin >> m;
for (int i = 1; i < m; ++i) {
pi += (2 * (productsumnum(i) / productsumden(i)));
}
res = (float)(pi);
std::cout << res;
return 0;
}