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 ...
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.