I am having issues with my greatest common denominator function in my code.

Ok I have been working on this for a while and little by little and with the help of you guys for one section I wrote this code so far. I wrote this to output a fraction in its lowest form and came up with a greatest common denominator function to use with my other function to get the answer. so far I added the new function and added it into my addition function and when I tested it out my output is giving my 1/0 for my answer every time. for example i enter my fractions and instead of 6/12 + 3/12 = 9/12 which is 3/4 in lowest form i get 1/0 for lowest form for everything. Here is the part of my code I'm working on so I can add it to the rest of my fucntions. I'm hoping a fresh set of eyes will help me. 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
// my greatest common denom function
int greatestCommonDenominator(int& new_numerator, int& new_denominator) 

{ 

int remainder ; 


while(new_denominator != 0) 

{ 

remainder = new_numerator % new_denominator ; 

new_numerator = new_denominator ; 

new_denominator = remainder ; 

} // end while loop 


return new_numerator ; 

} // close gcd function 

 

/* Now here is my addition function which calls the greatestCommon... function to complete the answer. */

 

void addition(int& numerator1, int& denominator1, int& numerator2, int& denominator2, int& new_numerator, int& new_denominator, int gcd, int reduced_numerator, int reduced_denominator) 

{

new_numerator = (numerator1 * denominator2) + (numerator2 * denominator1) ;

new_denominator = denominator1 * denominator2 ;

gcd = greatestCommonDenominator(new_numerator, new_denominator) ;

reduced_numerator = new_numerator / gcd ;

reduced_denominator = new_denominator / gcd ;

cout << endl

<< " Your answer is " << reduced_numerator << "/" << reduced_denominator << endl << endl ;

} // close addition function



Your GCD function is flawed. If you were to input 9/12, first it would take the remainder of 9 and 12, which is 3. Then it would make the numerator the current denominator (12) and the denominator the remainder (3). It would then repeat, making the numerator 3 and the denominator 0, and then exit. Thus, when you divide the numerator by the GCD, you would obtain 1. When you divide the denominator, you would obtain 0. Hence the answers of 1/0.
Topic archived. No new replies allowed.