Adding fractions using recursion

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Adding fractions using recursion

 
0
  #1
Apr 29th, 2009
Hello friends, I'm working on a program that takes a number input from the user and recursively sums the number 'n' to one. For example, inputting a 5 would sum 1/5 + 1/4 + 1/3+ 1/2 + 1/1. I have sucessfully gotten the fraction sequence to display but I am having trouble with the sum.
Here is my client program
  1. #include "Fraction.cpp"
  2. #include<iostream>
  3. using namespace std;
  4. Fraction SumOfSeries(int n);//adds fractions from num to 1
  5. int main()
  6. {
  7. int num;
  8. cout<<"Enter a number: ";
  9. cin>>num;
  10. cout<<"Sum of Series-> ";
  11. SumOfSeries(num);
  12. char wait;
  13. cout<<"\nEnter any key to end ";
  14. cin>>wait;
  15. return 0;
  16. }
  17. Fraction SumOfSeries(int n)
  18. {
  19. Fraction base(1,1);
  20. Fraction temp(1,n);
  21. if(n==1)
  22. {
  23. base.Display();
  24. return base;
  25. }
  26. else
  27. {
  28. for(int i=1; i<=n; i++)
  29. {
  30. base=base+temp;
  31. temp.Display();
  32. if(i!=n)
  33. cout<<" + ";
  34. return temp+SumOfSeries(n-1);
  35. }
  36. }
  37.  
  38. }
Here is my fraction class (you probably don't need to spend too much time looking at this.
  1. #include <iostream>
  2. using namespace std;
  3. class Fraction
  4. {public:
  5. Fraction();
  6. Fraction(int n, int d);
  7. void Assign(int n, int d);
  8. Fraction Recip();
  9. void Display();
  10. int GetNum();
  11. int GetDen();
  12. double Convert();
  13. Fraction operator+(Fraction f);
  14. Fraction operator*(Fraction f);
  15. Fraction operator-(Fraction f);
  16. Fraction operator/(Fraction f);
  17. bool operator>(Fraction f);
  18. bool operator<(Fraction f);
  19. bool operator==(Fraction f);
  20. void Reduce();
  21. int GCF();
  22. friend ostream&operator<<(ostream&outs, const Fraction f);
  23.  
  24. private:
  25. int num, den;
  26. };
  27.  
  28. Fraction::Fraction()
  29. {}
  30.  
  31. void Fraction::Assign(int n,int d)
  32. {
  33. num=n;
  34. den=d;
  35. }
  36. void Fraction::Display()
  37. {
  38. cout<<num<<"/"<<den;
  39. }
  40. Fraction Fraction::Recip()
  41. {
  42. Fraction temp(den,num);
  43. return temp;
  44. }
  45. int Fraction::GetNum()
  46. {
  47. return num;
  48. }
  49. int Fraction::GetDen()
  50. {
  51. return den;
  52. }
  53.  
  54. Fraction::Fraction(int n, int d)
  55. {
  56. if (d!=0)
  57. {num=n; den=d;}
  58. else cout<<"error: fraction is undefined\n";
  59. }
  60. Fraction Fraction::operator+(Fraction f)
  61. {
  62. Fraction temp;
  63. temp.num=num*f.den+f.num*den;
  64. temp.den=den*f.den;
  65. temp.Reduce();
  66. return temp;
  67. }
  68. Fraction Fraction::operator*(Fraction f)
  69. {
  70. Fraction temp;
  71. temp.num=num*f.num;
  72. temp.den=den*f.den;
  73. temp.Reduce();
  74. return temp;
  75. }
  76. Fraction Fraction::operator-(Fraction f)
  77. {
  78. Fraction temp;
  79. temp.num=num*f.den-f.num*den ;
  80. temp.den=den*f.den;
  81. temp.Reduce();
  82. return temp;
  83. }
  84. Fraction Fraction::operator/(Fraction f)
  85. {
  86. Fraction temp;
  87. temp.num=num*f.den;
  88. temp.den=den*f.num;
  89. temp.Reduce();
  90. return temp;
  91. }
  92. bool Fraction::operator>(Fraction f)
  93. {
  94. bool temp;
  95. if(Convert()>f.Convert())
  96. {temp=true;}
  97. else
  98. {temp=false;}
  99. return temp;
  100. }
  101. bool Fraction::operator<(Fraction f)
  102. {
  103. bool temp;
  104. if(Convert()<f.Convert())
  105. {temp=true;}
  106. else
  107. {temp=false;}
  108. return temp;
  109. }
  110. bool Fraction::operator==(Fraction f)
  111. {
  112. bool temp;
  113. if(Convert()==f.Convert())
  114. {temp=true;}
  115. else
  116. {temp=false;}
  117. return temp;
  118. }
  119. void Fraction::Reduce()
  120. {
  121. int gcf=GCF();
  122. num=num/gcf;
  123. den=den/gcf;
  124. }
  125. int Fraction::GCF()
  126. {
  127. int gcf;
  128. bool done=false;
  129. if(num<=den)
  130. gcf=num;
  131. else gcf=den;
  132. while(gcf>1&&!done)
  133. {
  134. if(num%gcf==0&&den%gcf==0)
  135. done=true;
  136. else gcf--;
  137. }
  138. return gcf;
  139. }
  140. double Fraction::Convert()
  141. {
  142. double deci;
  143. deci=static_cast<double>(num)/den;
  144. return deci;
  145. }
  146.  
  147. ostream&operator<<(ostream&outs, const Fraction f)
  148. {
  149. outs<<f.num<<"/"<<f.den;
  150. return outs;
  151. }
Thanks for any help.
Last edited by Grn Xtrm; Apr 29th, 2009 at 4:15 pm.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Adding fractions using recursion

 
1
  #2
Apr 29th, 2009
Firstly you are returning a Fraction type but not stroring or displaying it.

  1. Fraction frac=SumOfSeries(num);
  2. frac.display;

Secondly in Function SumOfSeries Why are you introducing a for loop?

  1. #include "Fraction.cpp"
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. Fraction SumOfSeries(int n);//adds fractions from num to 1
  6. int main()
  7. {
  8. int num;
  9. cout<<"Enter a number: ";
  10. cin>>num;
  11. cout<<"Sum of Series-> ";
  12. Fraction frac=SumOfSeries(num);
  13. frac.Display();
  14. char wait;
  15. cout<<"\nEnter any key to end ";
  16. cin>>wait;
  17. return 0;
  18. }
  19. Fraction SumOfSeries(int n)
  20. {
  21. Fraction base(1,1);
  22. Fraction temp(1,n);
  23. if(n==1)
  24. {
  25. return base;
  26. }
  27. else
  28. {
  29. return temp+SumOfSeries(n-1);
  30. }
  31.  
  32. }
This code returns the correct output.
All you need to do is write a print function which writes down... all the numbers such as

  1. writenumbers(int n)
  2. {
  3. for(int i=1; i<=n; i++)
  4. {
  5. Fraction temp(1,n);
  6. temp.Display();
  7. if(i!=n)
  8. cout<<" + ";
  9. }
  10. }
Last edited by Sky Diploma; Apr 29th, 2009 at 8:54 pm.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Adding fractions using recursion

 
0
  #3
Apr 30th, 2009
Thanks for the reply but I need to use recursion (i.e. the function has to call itself) to solve this problem.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Adding fractions using recursion

 
0
  #4
Apr 30th, 2009
Sorry, I followed your first piece of advice and got the sum to display. Thanks alot for the help.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Reply

Tags
recursion

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for recursion
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC