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.

thank you in advance!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include<iostream>
#include<cstdlib>
#include<cmath>
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<b) cout << "e<b\n";

    system("PAUSE");
    return 0;
}
Last edited on
For starters, what happens to values of members fraction.n and fraction.d in this function?

1
2
3
4
5
6
7
8
9
10
11
12
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:
1
2
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:
1
2
3
4
5
6
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.

1
2
3
4
5
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:

1
2
3
4
5
6
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
2
3
4
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.