What am I trying to do?
I am trying to calculate the equivalent resistance of this resistance setup:
R1, R2, R3, R4
are of type int
given by the user. The computer should compute the equivalent resistance Req
of type int
rounding it to the nearest integer.
What have I tried?
I have tried rounding using the ceil
function for rounding.
#include <iostream>
#include <cmath>
int main(){
double R1, R2, R3, R4;
std::cout << "enter R1: " << std::endl;
std::cin >> R1;
std::cout << "enter R2: " << std::endl;
std::cin >> R2;
std::cout << "enter R3: " << std::endl;
std::cin >> R3;
std::cout << "enter R4: " << std::endl;
std::cin >> R4;
double R12;
R12 = R1 + R2;
double R34;
R34 = R3 + R4;
int Req;
Req = ceil((R12*R34)/(R12+R34));
std::cout << "the equivalent resistance is: " << Req << std::endl;
return 0;
}
What's the issue?
I wanted to try doing it without using double
type variables, but if I turn R12
and R34
into int
, then ceil
doesn't work anymore.
Why isn't this question a duplicate? I only found questions similar to this one but they all have a major drawback: Int overflowing, which in my case is likely to happen in case the resistance is entered in mOhm, so to speak.