| | |
Adding fractions using recursion
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
Here is my fraction class (you probably don't need to spend too much time looking at this.
Thanks for any help.
Here is my client program
C++ Syntax (Toggle Plain Text)
#include "Fraction.cpp" #include<iostream> using namespace std; Fraction SumOfSeries(int n);//adds fractions from num to 1 int main() { int num; cout<<"Enter a number: "; cin>>num; cout<<"Sum of Series-> "; SumOfSeries(num); char wait; cout<<"\nEnter any key to end "; cin>>wait; return 0; } Fraction SumOfSeries(int n) { Fraction base(1,1); Fraction temp(1,n); if(n==1) { base.Display(); return base; } else { for(int i=1; i<=n; i++) { base=base+temp; temp.Display(); if(i!=n) cout<<" + "; return temp+SumOfSeries(n-1); } } }
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class Fraction {public: Fraction(); Fraction(int n, int d); void Assign(int n, int d); Fraction Recip(); void Display(); int GetNum(); int GetDen(); double Convert(); Fraction operator+(Fraction f); Fraction operator*(Fraction f); Fraction operator-(Fraction f); Fraction operator/(Fraction f); bool operator>(Fraction f); bool operator<(Fraction f); bool operator==(Fraction f); void Reduce(); int GCF(); friend ostream&operator<<(ostream&outs, const Fraction f); private: int num, den; }; Fraction::Fraction() {} void Fraction::Assign(int n,int d) { num=n; den=d; } void Fraction::Display() { cout<<num<<"/"<<den; } Fraction Fraction::Recip() { Fraction temp(den,num); return temp; } int Fraction::GetNum() { return num; } int Fraction::GetDen() { return den; } Fraction::Fraction(int n, int d) { if (d!=0) {num=n; den=d;} else cout<<"error: fraction is undefined\n"; } Fraction Fraction::operator+(Fraction f) { Fraction temp; temp.num=num*f.den+f.num*den; temp.den=den*f.den; temp.Reduce(); return temp; } Fraction Fraction::operator*(Fraction f) { Fraction temp; temp.num=num*f.num; temp.den=den*f.den; temp.Reduce(); return temp; } Fraction Fraction::operator-(Fraction f) { Fraction temp; temp.num=num*f.den-f.num*den ; temp.den=den*f.den; temp.Reduce(); return temp; } Fraction Fraction::operator/(Fraction f) { Fraction temp; temp.num=num*f.den; temp.den=den*f.num; temp.Reduce(); return temp; } bool Fraction::operator>(Fraction f) { bool temp; if(Convert()>f.Convert()) {temp=true;} else {temp=false;} return temp; } bool Fraction::operator<(Fraction f) { bool temp; if(Convert()<f.Convert()) {temp=true;} else {temp=false;} return temp; } bool Fraction::operator==(Fraction f) { bool temp; if(Convert()==f.Convert()) {temp=true;} else {temp=false;} return temp; } void Fraction::Reduce() { int gcf=GCF(); num=num/gcf; den=den/gcf; } int Fraction::GCF() { int gcf; bool done=false; if(num<=den) gcf=num; else gcf=den; while(gcf>1&&!done) { if(num%gcf==0&&den%gcf==0) done=true; else gcf--; } return gcf; } double Fraction::Convert() { double deci; deci=static_cast<double>(num)/den; return deci; } ostream&operator<<(ostream&outs, const Fraction f) { outs<<f.num<<"/"<<f.den; return outs; }
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!
URL on facebook!
Firstly you are returning a Fraction type but not stroring or displaying it.
Secondly in Function SumOfSeries Why are you introducing a for loop?
This code returns the correct output.
All you need to do is write a print function which writes down... all the numbers such as
C++ Syntax (Toggle Plain Text)
Fraction frac=SumOfSeries(num); frac.display;
Secondly in Function SumOfSeries Why are you introducing a for loop?
C++ Syntax (Toggle Plain Text)
#include "Fraction.cpp" #include<iostream> using namespace std; Fraction SumOfSeries(int n);//adds fractions from num to 1 int main() { int num; cout<<"Enter a number: "; cin>>num; cout<<"Sum of Series-> "; Fraction frac=SumOfSeries(num); frac.Display(); char wait; cout<<"\nEnter any key to end "; cin>>wait; return 0; } Fraction SumOfSeries(int n) { Fraction base(1,1); Fraction temp(1,n); if(n==1) { return base; } else { return temp+SumOfSeries(n-1); } }
All you need to do is write a print function which writes down... all the numbers such as
C++ Syntax (Toggle Plain Text)
writenumbers(int n) { for(int i=1; i<=n; i++) { Fraction temp(1,n); temp.Display(); if(i!=n) cout<<" + "; } }
Last edited by Sky Diploma; Apr 29th, 2009 at 8:54 pm.
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!
URL on facebook!
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!
URL on facebook!
![]() |
Other Threads in the C++ Forum
- Previous Thread: c++ help2
- Next Thread: C++ Prog..........urgent
| Thread Tools | Search this Thread |
Tag cloud for recursion






