### help with my fraction class that i created

i created a fraction class and in the main function included the necessary checks to see if my class works properly but it looks like there are some things that aren't correct, i need to fix one problem at a time. the first thing i need help with is my reduce function (reduce the fraction) and convert function (convert fraction to decimal) becasue it is not displaying the correct values. if you copy and pase my following program and run it in c++ you'll see why it is wrong.

here is my program with the class function and the necessary checks in main funciton.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273`` ``````#include #include #include using namespace std; class fraction { private: int n, d; int gcd(int, int); //added these two ints public: fraction(); fraction(int); fraction(int, int); int num(); int denom(); fraction reduce(); double convert(); }; fraction::fraction() { n=0, d=1; } fraction::fraction(int x) { n=x, d=1; } fraction::fraction(int x, int y) { n=x, d=y; } int fraction::gcd(int n, int d) //added these two ints { while(d!=0) { int t=n%d; n=d; d=t; } if(n < 0) n*=-1; return n; } int fraction::num() { return n; } int fraction::denom() { return d; } fraction fraction::reduce() //is this correct? { int A=n/gcd(n, d); //also added the arguements in both of these gcds. int B=d/gcd(n, d); fraction C(A, B); return C; } double fraction::convert() //is this correct? { double C=(double)n/d; return C; } fraction operator+(fraction A) { A.reduce(); return A; } fraction operator-(fraction A) { int a=-A.num(); int b=A.denom(); fraction C(a, b); C.reduce(); return C; } fraction operator+(fraction A, fraction B) { int a=(A.num()*B.denom())+(B.num()*A.denom()); int b=A.denom()*B.denom(); fraction C(a, b); C.reduce(); return C; } fraction operator-(fraction A, fraction B) { return +A+(-B); } fraction operator*(fraction A, fraction B) { int a=A.num()*B.num(); int b=A.denom()*B.denom(); fraction C(a, b); C.reduce(); return C; } fraction operator/(fraction A, fraction B) { int a=A.num()*B.denom(); int b=A.denom()*B.num(); fraction C(a, b); C.reduce(); return C; } void operator+=(fraction& A, fraction B) { A=A+B; A.reduce(); } void operator-=(fraction& A, fraction B) { A=A-B; A.reduce(); } void operator*=(fraction& A, fraction B) { A=A*B; A.reduce(); } void operator/=(fraction& A, fraction B) { A=A/B; A.reduce(); } bool operator<(fraction A, fraction B) { if(A.convert() < B.convert()) return true; else return false; } bool operator>(fraction A, fraction B) { if(A.convert() > B.convert()) return true; else return false; } bool operator>=(fraction A, fraction B) { if(A < B) return false; else return true; } bool operator<=(fraction A, fraction B) { if(A > B) return false; else return true; } bool operator==(fraction A, fraction B) { if(A.convert() == B.convert()) return true; else return false; } bool operator!=(fraction A, fraction B) { if(A==B) return false; else return true; } ostream& operator<<(ostream& out, fraction A) { out << A.num() << "/" << A.denom(); return out; } istream& operator>>(istream& in, fraction& A) { int a, b; char s; in >> a >> s >> b; fraction B(a, b); A=B; return in; } int main() { fraction a; //a = 0/1 fraction b(5); //b=5/1 fraction c(6,8); //c=6/8 cout << "a=" << a << ", b=" << b << ", c=" << c << endl; //should display a=0, b=5, c=6/8 cout << "The numerator of c is " << c.num() << " and the denominator is " << c.denom() << endl; //should show 6 then 8 cout << "As a decimal, c=" << c.convert() << endl; //should show 0.75 c.reduce(); cout << "When you reduce c, you get " << c << endl; //should show 3/4 fraction d; cout << "Enter a fraction: "; cin >> d; //tests >> cout << "d=" << d << endl; //should display the fraction you just entered cout << "+d=" << +d << ", -d=" << -d << endl; //test unary +/- cout << "c+d=" << c+d << endl; //check if this is true (should be reduced) cout << "c-d=" << c-d << endl; //check if this is true (should be reduced) cout << "c*d=" << c*d << endl; //check if this is true (should be reduced) cout << "c/d=" << c/d << endl; //check if this is true (should be reduced) c+=d; cout << "c=" << c << endl; c-=d; cout << "c=" << c << endl; c*=d; cout << "c=" << c << endl; c/=d; cout << "c=" << c << endl; fraction e(1,1); if(c!=e) cout << "c!=e\n"; c/=c; //c=1/1 if(c==e) cout << "c==e\n"; if(b>e) cout << "b>e\n"; if(c>=e) cout << "c>=e\n"; if(c<=e) cout << "c<=e\n"; if(e
Last edited on
For starters, what happens to values of members fraction.n and fraction.d in this function?

 ``123456789101112`` ``````int fraction::gcd() //this always finds the gcd, i already tested this out. { while(d!=0) { int t=n%d; n=d; d=t; } if(n < 0) n*=-1; return n; }``````

I think this (in line 95) is a typo:

`int a=(A.num()*B.denom())+(B.num()+A.denom());`
Last edited on
The problem with convert() is at line 72, the compiler generates code similar to:
 ``12`` ``````int tmp = n/d; double C = tmp;``````

and since integer division produces an integer result, you get the wrong answer. To fix it, convert one value to double:
`double C = (double)n / d;`
PCrumley48 pointed out the problem with gcd(). Think you'll find it easier to write the code if gcd() takes the two numbers as arguments:
int gcd(int a, int b);
That way you can find the GCD of any two numbers.

Some other hints:

You can collapse all the constructors down to one if you use default values for the parameters:
`fraction(int num=0, int den=1);`

Code +=, -=, *= and /= first. Once you have these, +, -, * and / are easy, for example:
 ``123456`` ``````fraction operator+(const fraction &right) const { fraction result(*this); result += right; return result; }``````

i changed my program above with your suggestions and now i get the correct answer when it converts 6/8 to a decimal, however the reduce function is still not working properly. before it would reduce 6/8 to 2/0 now it keeps it as 6/8? does anyone know how to fix it?
i tested my program with my professors exe file and everything including the operators overloaded work properly except my reduce function. because for all of the tests that are in the main function i am getting the correct answers but they are just not reduced.
Shouldn't reduce() change the current fraction rather than returning a new one? In other words, shouldn't it be: `void reduce();` instead of fraction `reduce();`?
i changed it to a void function now it reduces 6/8 to 3/8.

 ``12345`` ``````void fraction::reduce() { n=n/gcd(n, d); d=d/gcd(n, d); }``````
When you do `n = n / gcd(n,d)`

you are using 6/8 for the parameters to gcd, which gives you a new numerator of 3, but leaves the denominator at 8.

gcd(3,8) returns 1, so the fraction remains the same.

You need to call gcd once, into a local variable, then divide n and d by the same value.
Last edited on
i changed the reduce function to:

 ``123456`` ``````void fraction::reduce() { int g=gcd(n, d); n=n/g; d=d/g; }``````

and now my class works perfectly
thanks everyone for their help

but i have one more question,
can someone explain why in the private and public function definitions there are certain definitions where you have to include the inputs and others where you leave the parenthesis empty
for example:

 ``12345678910111213141516`` ``````class fraction { private: int n, d; int gcd(int, int); //why does this definition need inputs but not num, denom, reduce, //or convert? public: fraction(); fraction(int); fraction(int, int); int num(); int denom(); fraction reduce(); double convert(); };``````

i tried to use ` gcd(); ` as the definition instead and the reduced fraction it would give me was 1/0.

and how do i know which function changes the values of n and d?
for example, the maximal constructor ` fraction(int, int) ` changes the values of n and d based on what the user or programmer inputs even though i didn't use an & sign?
but the ` gcd(int, int) ` function simply finds the gcd of n and d but doesnt change their values?

i thought you should always leave the parenthesis in definitions empty if all you're using are the variables in the class because the class knows what variables the functions are refering too?
Last edited on
 can someone explain why in the private and public function definitions there are certain definitions where you have to include the inputs and others where you leave the parenthesis empty

You don't really "have" to include the inputs, but there are some issues. If you don't send the values you intend to use to the method in the declaration, you'll need to protect your base variables somehow unless you intend to modify them:

 ``12345678910111213`` ``````int fraction::gcd(int n, int d) //added these two ints; this creates locally scoped variables n and d { while(d!=0) // because you added these as parameters, //d is now a local variable and the class member d will not be modified { int t=n%d; // n is now local same reason, so class member n is unchanged. n=d; d=t; } if(n < 0) n*=-1; return n; }``````

If you wanted to avoid the parameter declaration you could do this:
 ``1234567891011121314`` ``````int fraction::gcd() { int f_d = d, f_n = n; //using locally declared variables prevents // undesirable changes to members n, d while(f_d!=0) { int t=f_n % f_d; f_n=f_d; f_d=t; } if(f_n < 0) f_n*=-1; return f_n; }``````

 the maximal constructor fraction(int, int) changes the values of n and d based on what the user or programmer inputs even though i didn't use an & sign? but the gcd(int, int) function simply finds the gcd of n and d but doesnt change their values?

Your constructor accepts parameters x and y
 ``1234`` ``````fraction::fraction(int x, int y) { n=x, d=y; }``````

and assigns x to class member` int n`, y to class member `int d`.

gcd(int n, int d) creates locally scoped n and d, so isolates them from class members n and d.

This whole thing is a matter of scope, and it really pays to be aware of what the scope of each variable you access is, and what you mean it to be.
thank you for the explanation, it seems like i always overlook the simple things in programming like how int/int will always give you an int. i understande what scopes are but for some reason they seem a little more confusing when dealing with scopes and classes.
Topic archived. No new replies allowed.