Reducing fractions

so I have looked on several forms but I am just not understanding how i can implement Euclid's Algoritm into my code.
This is Euclid's Algoritm.
1
2
3
4
5
6
7
8
9
int gcd( int num1, int num2 )
{
	int remainder = num2 % num1;
	
	if ( remainder != 0 )
		return gcd( remainder,num1 );
	
	return num1;
}


And this my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED

class fraction
{
public:
    fraction (); // default constructor
    fraction (int,int);
    void print() const;
    fraction MultipliedBy(fraction) const;
    fraction isGreaterThan(fraction) const;
    fraction AddedTo(fraction) const;
    fraction Subtract(fraction) const;
    fraction isEqualTo(fraction) const;
    fraction DividedBy(fraction) const;
private:
    int numerator;
    int denominator;
};

#endif // FRACTION_H_INCLUDED


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

#include <iostream>
#include "fraction.h"

using namespace std;

    fraction::fraction ()// default constructor
    {
        numerator=0;
        denominator=1;
    }
    
    fraction::fraction (int newNumerator,int newDenominator)
    {
        denominator=newDenominator;
        numerator=newNumerator;
    }
    
    void fraction::print() const
    {
        cout<<numerator<<"/"<<denominator;
    }
    
    fraction fraction::MultipliedBy(fraction f) const
    {
        /*not sure where but somewhere here is
        where i think i need add Euclid's Algoritm*/
        f.denominator*=denominator;
        f.numerator*=numerator;
        return f;

    }


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
#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
    fraction f1(9,8); //calling a parameterized class constructor
    fraction f2(2,3); //calling a parameterized class constructor
    fraction result;  //calling a default class constructor
    fraction f3; //calling a default class constructor

    cout << "The result starts off at ";
    result.print(); //calling an observer function
    cout << endl;

    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.MultipliedBy(f2); //a class binary operation - function
    result.print();
    cout << endl;

    f3 = result; //assignment 


And this is my current output. The 18/24 needs to be 3/4.

The result starts off at 0/1
The product of 9/8 and 2/3 is 18/24

Process returned 0 (0x0)   execution time : 0.020 s
Press any key to continue.

Look at this:

http://en.wikipedia.org/wiki/Euclidean_algorithm

Accordingly it has to be:
1
2
3
4
5
6
7
int gcd( int num1, int num2 )
{
    if (num2 == 0)
       return num1;
    else
       return gcd(num2, num1 % num2);
}
And if I undersrand this correctly then a is my numerator and b is my denominator but I cant just replace a with numerator and b with denominator right?
Topic archived. No new replies allowed.