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.

Recommended Answers

All 5 Replies


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.

Why on earth do you want the lowest common denominator to add the fractions?? Can't you just add them just like any double value?

Well, in order to add them, I need to get them to have the same denominator, correct?

If they have the same denominator, thats no problem.

Am I going in the wrong direction with this?

Let's go with your example.

/*********************************************
   * 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                            *
   **********************************************/

Can't you add it and then get it's reciprocal like this?

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

WolfPack, thanks, that last suggestion of yours made sense. I think I can build off that. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.