Lowest Common Denominator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 12
Reputation: riscphree is an unknown quantity at this point 
Solved Threads: 0
riscphree riscphree is offline Offline
Newbie Poster

Lowest Common Denominator

 
0
  #1
Apr 27th, 2006
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 ...

  1. int main(void)
  2. {
  3.  
  4. int num_of_resis; // number of resistors
  5. int values[256]; // holds values for resistors
  6. int i; // specifys data range
  7. int index; // index into data
  8. int div;
  9. int var1, var2, count;
  10.  
  11. cout << "How many resistors: ";
  12. cin >> num_of_resis;
  13. for(i=0;i < num_of_resis;i++){ // stores resistor values
  14. cout << "What is the value of one resistor?: ";
  15. cin >> values[i]; // used for debug
  16. cout << "value is " << values[i] << endl;
  17. }
  18.  
  19. /*********************************************
  20.   * math: 1/R = 1/R1 + 1/R2 + 1/R3 ... + 1/Rn *
  21.   * ex: 1/R = 1/400 + 1/200 *
  22.   * 1/R = 3/400 *
  23.   * R = 400/3 *
  24.   * R = 133.33 ohms *
  25.   **********************************************/
  26.  
  27. cout << " ---------- " << endl; // make output look somewhat better
  28.  
  29. for(i=0;i<num_of_resis;i++){
  30. count = div = values[i];
  31. if(values[i] < 1){
  32. cout << "error\n";
  33. cout << "cannot be negative\n";
  34. break;
  35. }
  36. if(values[i] == 0){
  37. cout << "continuing\n";
  38. cout << "value cannot be 0";
  39. break;
  40. }else{
  41. var1 = values[i] / values[i++];
  42. cout << "values[i] is " << values[i] << endl;
  43. cout << var1 << endl;
  44. }
  45. }
  46.  
  47. return 0;
  48. }

Someone suggested using Euclid's algorithm, but I don't know exactly how that would work in this situation.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Lowest Common Denominator

 
0
  #2
Apr 27th, 2006
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.
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?
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 12
Reputation: riscphree is an unknown quantity at this point 
Solved Threads: 0
riscphree riscphree is offline Offline
Newbie Poster

Re: Lowest Common Denominator

 
0
  #3
Apr 27th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Lowest Common Denominator

 
0
  #4
Apr 27th, 2006
Let's go with your example.
  1. /*********************************************
  2.   * math: 1/R = 1/R1 + 1/R2 + 1/R3 ... + 1/Rn *
  3.   * ex: 1/R = 1/400 + 1/200 *
  4.   * 1/R = 3/400 *
  5.   * R = 400/3 *
  6.   * R = 133.33 ohms *
  7.   **********************************************/
Can't you add it and then get it's reciprocal like this?
  1. 1/R = 1/400 + 1/200
  2. 1/R = 0.0025 + 0.005
  3. 1/R = 0.0075
  4. R = 1 / 0.0075
  5. R = 133.33333333
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Lowest Common Denominator

 
0
  #5
Apr 27th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 12
Reputation: riscphree is an unknown quantity at this point 
Solved Threads: 0
riscphree riscphree is offline Offline
Newbie Poster

Re: Lowest Common Denominator

 
0
  #6
Apr 27th, 2006
WolfPack, thanks, that last suggestion of yours made sense. I think I can build off that.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC