First Exercise
This commit is contained in:
13
Informatik_I/Examples/Week_1/README.md
Normal file
13
Informatik_I/Examples/Week_1/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Task
|
||||
|
||||
Write a program which reads in an integer a larger than `1000` and outputs its last three digits. For example, if `a=14325`, the output should be `3 2 5`.
|
||||
|
||||
_Hint_: You need to use integer division and modulo operators.
|
||||
|
||||
# Input
|
||||
|
||||
An integer larger than `1000`.
|
||||
|
||||
# Output
|
||||
|
||||
Last three digits separated by spaces.
|
22
Informatik_I/Examples/Week_1/main.cpp
Normal file
22
Informatik_I/Examples/Week_1/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
|
||||
int a;
|
||||
std::cin >> a; // get input of for a
|
||||
|
||||
if (a >= 1000) { // check if input is greater than 1000
|
||||
int b = a % 10; // get last digit
|
||||
a /= 10;
|
||||
int c = a % 10; // get second digit
|
||||
a /= 10;
|
||||
int d = a % 10; // get first digit
|
||||
|
||||
std::cout << d << " " << c << " "
|
||||
<< b; // output first, second, and last digit
|
||||
} else { // if input not greater than 1000 return 0
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
17
Informatik_I/Exercise_1/Task_1/README.md
Normal file
17
Informatik_I/Exercise_1/Task_1/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Task
|
||||
|
||||
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.
|
||||
|
||||
1. a = b = 5
|
||||
1. 1 = a
|
||||
1. ++a + b++
|
||||
1. a + b = c + d
|
||||
1. a = 2 b
|
||||
|
||||
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.
|
||||
|
||||
- Determine the values of the expressions that you have identified as valid and explain how these values are obtained.
|
23
Informatik_I/Exercise_1/Task_1/main.md
Normal file
23
Informatik_I/Exercise_1/Task_1/main.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Task 1
|
||||
|
||||
---
|
||||
|
||||
### Expression 1: `a = b = 5`
|
||||
|
||||
The expression is valid and will be accepted by the CPP compiler. It will result with the l-values `a = 5` and `b = 5`. This is due to the fact the the `=` opperation is read from right to left so `b = 5` will run first and `a = b` afterwards.
|
||||
|
||||
### Expression 2: `1 = a`
|
||||
|
||||
The expression is invalid.
|
||||
|
||||
### Expression 3: `++a + b++`
|
||||
|
||||
The expression is valid and will be accepted by the CPP compiler. It will result in a r-value since the result is not assigned to a variable.
|
||||
|
||||
### Expression 4: `a + b = c + d`
|
||||
|
||||
The expression is invalid.
|
||||
|
||||
### Expression 5: `a = 2 b`
|
||||
|
||||
The expression is valid and will be accepted by the CPP compiler. It will result with the l-value `a = 2 b`. This is valid since a l-value can be a r-value.
|
24
Informatik_I/Exercise_1/Task_2/README.md
Normal file
24
Informatik_I/Exercise_1/Task_2/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# 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._
|
||||
|
||||
_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):
|
||||
|
||||
```txt
|
||||
# Lines starting with # are comments. Spaces is ignored.
|
||||
# Lines starting with a whitespace before # will not be a comment.
|
||||
|
||||
# Convert to decimal:
|
||||
0b1 = ?
|
||||
0b10 = ?
|
||||
0b000001 = ?
|
||||
0b101010 = ?
|
||||
|
||||
# Convert to binary:
|
||||
7 = ?
|
||||
11 = ?
|
||||
28 = ?
|
||||
1024 = ?
|
||||
```
|
14
Informatik_I/Exercise_1/Task_2/submission.txt
Normal file
14
Informatik_I/Exercise_1/Task_2/submission.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
# Lines starting with # are comments. Spaces are ignored.
|
||||
# Lines starting with a whitespace before # will not be a comment.
|
||||
|
||||
# Convert to decimal:
|
||||
0b1 = 1
|
||||
0b10 = 2
|
||||
0b000001 = 1
|
||||
0b101010 = 42
|
||||
|
||||
# Convert to binary:
|
||||
7 = 0b111
|
||||
11 = 0b1011
|
||||
28 = 0b11100
|
||||
1024 = 0b10000000000
|
18
Informatik_I/Exercise_1/Task_3/README.md
Normal file
18
Informatik_I/Exercise_1/Task_3/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Task
|
||||
|
||||
Write a program resistance.cpp that computes the equivalent resistance of the following wiring:
|
||||
|
||||

|
||||
|
||||
We assume that $R_1$, $R_2$, $R_3$, and $R_4$ have an integer valued resistance. After input of the four values, the program should output the result arithmetically rounded to the nearest integer.
|
||||
|
||||
**Remark:** In order to facilitate the task, you may want to:
|
||||
|
||||
- Conceptually divide the task into sub tasks. For example, start with computation of serial resistors $R_{12}$ and $R_{34}$.
|
||||
- Solve the task first naively using default rounding and then think about how to accomplish arithmetic rounding. Recall that $\text{round}(x) = [x + 0.5] \text{ }\forall \text{ } x \in \mathbb{R}$, i.e., a real number can be rounded arithmetically by adding $0.5$ to it and then rounding down. For example, $\text{round}(8,6) = [8.6 + 0.5] = [9.1] = 9$.
|
||||
|
||||
You can find formulas for computing the total resistance in this [Wikipedia article](https://en.wikipedia.org/wiki/Resistor#Series_and_parallel_resistors).
|
||||
|
||||
**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.
|
25
Informatik_I/Exercise_1/Task_3/resistance.cpp
Normal file
25
Informatik_I/Exercise_1/Task_3/resistance.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
int r1;
|
||||
int r2;
|
||||
int r3;
|
||||
int r4;
|
||||
int res;
|
||||
|
||||
std::cin >> r1;
|
||||
std::cin >> r2;
|
||||
std::cin >> r3;
|
||||
std::cin >> r4;
|
||||
|
||||
int r12 = r1 + r2;
|
||||
int r34 = r3 + r4;
|
||||
|
||||
res = (r12 * r34 + ((r12 + r34) / 2)) /
|
||||
(r12 + r34); // By adding the average of r12 and r34, the result is
|
||||
// increased enough to get the correct rounded number
|
||||
|
||||
std::cout << res;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
Informatik_I/Exercise_1/Task_3/resistance.png
Normal file
BIN
Informatik_I/Exercise_1/Task_3/resistance.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.4 KiB |
@@ -1,3 +0,0 @@
|
||||
# Informatik I
|
||||
|
||||
In diesem Verzeichnis findest du die Projekte für Informatik I.
|
Reference in New Issue
Block a user