944,164 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3518
  • C++ RSS
Apr 27th, 2006
0

Lowest Common Denominator

Expand Post »
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 ...

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
riscphree is offline Offline
12 posts
since Dec 2005
Apr 27th, 2006
0

Re: Lowest Common Denominator

Quote 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?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 27th, 2006
0

Re: Lowest Common Denominator

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
riscphree is offline Offline
12 posts
since Dec 2005
Apr 27th, 2006
0

Re: Lowest Common Denominator

Let's go with your example.
C++ Syntax (Toggle Plain Text)
  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?
C++ Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 27th, 2006
2

Re: Lowest Common Denominator

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).
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Apr 27th, 2006
0

Re: Lowest Common Denominator

WolfPack, thanks, that last suggestion of yours made sense. I think I can build off that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
riscphree is offline Offline
12 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with an array problem
Next Thread in C++ Forum Timeline: Please Help C++ Connect four program due tonight by 12 (2)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC