Exercise 3

Added Exercise 3 with solutions and README to Repository
This commit is contained in:
JirR02 2025-03-07 23:04:40 +01:00
parent b9d258de44
commit 7b86ebfe6a
10 changed files with 323 additions and 0 deletions

View File

@ -0,0 +1,24 @@
_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

@ -0,0 +1,43 @@
#include "loop.h"
#include <iostream>
// Fill out the file with the required answers
/*
Subtask 1: describe what the code snippet computes
It calculates the factorial of the number inputed by the user.
*/
/*
Subtask 2: decide which of the other two kind of loops would fit better, and
describe why.
Just a simple for loop would suffice the task of this code snippet, as we can
define the condition in the for loop as well as the expression. This
simplifies the code alot.
Of course we could also include the edge case where the input n is 0 or
smaller than zero. In this case an if statement would be more of use since we
have to seperate the cases.
*/
/*
Subtask 3: update the function below by rewriting the snippet into the
loop you specified in (2)
*/
void loop() {
int n;
std::cin >> n;
int f = 1;
for (; n > 0; --n)
f = f * n;
std::cout << f << std::endl;
}

View File

@ -0,0 +1,23 @@
_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

@ -0,0 +1,48 @@
#include "loop.h"
#include <iostream>
// Fill out the file with the required answers
/*
Subtask 1: describe what the code snippet computes
It is a very simple calculator which adds up the 2 number which are inputed by
the user.
*/
/*
Subtask 2: decide which of the other two kind of loops would fit better, and
describe why.
A while loop would be sufficient for the task of this code snippet, since we
only have to check, if the user wants to use the calculator again or not.
Of course, as in the last task we could look at the edge case. We could also
add an if expression to elimnate the posibility to input other numbers other
than 0 and 1 for the again variable.
*/
/*
Subtask 3: update the function below by rewriting the snippet into the
loop you specified in (2).
Note: print the control message "Again? (0/1)" using the same format used
in the snippet.
*/
void loop() {
int again = 1;
while (again != 0) {
int i1, i2;
std::cin >> i1 >> i2;
std::cout << i1 + i2 << "\n";
std::cout << "Again? (0/1)\n";
std::cin >> again;
}
}

View File

@ -0,0 +1,27 @@
_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,54 @@
#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

@ -0,0 +1,19 @@
# 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,30 @@
#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

@ -0,0 +1,20 @@
# 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,35 @@
#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;
}