| | |
Lowest Common Denominator
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2005
Posts: 12
Reputation:
Solved Threads: 0
In case anyone has this book, I'll mention it. I'm trying to complete Exercise 8-2 from Oreilly's Practical C++ Programming. The problem is to total the resistance of n resistors in parallel. The forumla for this is 1/R = 1/R1 + 1/R2+ ... 1/Rn
For example, say you have one 400Ohm and one 200Ohm resistors: 1/R = 1/400 + 1/200 which, when you do the math, comes out to R = 400/3 = 133.33Ohms
The problem I am having with with the algorithm of determining how to get the lowest common denominator in order to be able to easily add the fractions. Below is a start of what I got, but I have no idea where to start with the math ...
Someone suggested using Euclid's algorithm, but I don't know exactly how that would work in this situation.
For example, say you have one 400Ohm and one 200Ohm resistors: 1/R = 1/400 + 1/200 which, when you do the math, comes out to R = 400/3 = 133.33Ohms
The problem I am having with with the algorithm of determining how to get the lowest common denominator in order to be able to easily add the fractions. Below is a start of what I got, but I have no idea where to start with the math ...
C++ Syntax (Toggle Plain Text)
int main(void) { int num_of_resis; // number of resistors int values[256]; // holds values for resistors int i; // specifys data range int index; // index into data int div; int var1, var2, count; cout << "How many resistors: "; cin >> num_of_resis; for(i=0;i < num_of_resis;i++){ // stores resistor values cout << "What is the value of one resistor?: "; cin >> values[i]; // used for debug cout << "value is " << values[i] << endl; } /********************************************* * math: 1/R = 1/R1 + 1/R2 + 1/R3 ... + 1/Rn * * ex: 1/R = 1/400 + 1/200 * * 1/R = 3/400 * * R = 400/3 * * R = 133.33 ohms * **********************************************/ cout << " ---------- " << endl; // make output look somewhat better for(i=0;i<num_of_resis;i++){ count = div = values[i]; if(values[i] < 1){ cout << "error\n"; cout << "cannot be negative\n"; break; } if(values[i] == 0){ cout << "continuing\n"; cout << "value cannot be 0"; break; }else{ var1 = values[i] / values[i++]; cout << "values[i] is " << values[i] << endl; cout << var1 << endl; } } return 0; }
Someone suggested using Euclid's algorithm, but I don't know exactly how that would work in this situation.
•
•
•
•
Originally Posted by riscphree
The problem I am having with with the algorithm of determining how to get the lowest common denominator in order to be able to easily add the fractions.
Someone suggested using Euclid's algorithm, but I don't know exactly how that would work in this situation.
バルサミコ酢やっぱいらへんで
Let's go with your example.
Can't you add it and then get it's reciprocal like this?
C++ Syntax (Toggle Plain Text)
/********************************************* * math: 1/R = 1/R1 + 1/R2 + 1/R3 ... + 1/Rn * * ex: 1/R = 1/400 + 1/200 * * 1/R = 3/400 * * R = 400/3 * * R = 133.33 ohms * **********************************************/
C++ Syntax (Toggle Plain Text)
1/R = 1/400 + 1/200 1/R = 0.0025 + 0.005 1/R = 0.0075 R = 1 / 0.0075 R = 133.33333333
バルサミコ酢やっぱいらへんで
In C and C++, the 'double' datatype can be used to store approximations of the real numbers. For example,
double x = 3.5;. Use doubles to store all your resistances, and then you can do approximate arithmetic on them. These approximations can store about fourteen decimal digits of precision. They're like numbers in a calculator (where if you multiply (1 / 3) * 3 you're liable to get 0.9999999999999). All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- Why do people wish for tableless with CSS? (HTML and CSS)
Other Threads in the C++ Forum
- Previous Thread: Need help with an array problem
- Next Thread: Please Help C++ Connect four program due tonight by 12 (2)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






