First Exercise

This commit is contained in:
2025-02-20 23:51:20 +01:00
parent 2b700071aa
commit d580b385e5
27 changed files with 207 additions and 36 deletions

View File

@@ -0,0 +1,18 @@
# Task
Write a program resistance.cpp that computes the equivalent resistance of the following wiring:
![circuit](./resistance.png)
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.

View 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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB