#+TITLE: Task 3: Equivalent Resistance
#+AUTHOR: JirR02

* Task

Write a program resistance.cpp that computes the equivalent resistance of the following wiring:

[[./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 [[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 =if-else= and any other branches is forbidden.

* Solution

#+begin_src cpp
#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;
}
#+end_src

-—---

Made by JirR02 in Switzerland 🇨🇭
