954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with GCD algorithm

I've been working on this class for about 2-3 weeks and I got everything done except the reducing part. I can't figure it out , I know you have to use the GCD algorithm , but I cannot figure out how to use it :o , Can someone please tell me how to use it ? I know I have to use it in the reduce member but cannot figure out how to use it or how the syntax is supposed to be. Heres my Fraction.H , Fraction.cpp , and main.cpp files

// Fraction.H //
#include
#include
using namespace std;

class Fraction
{
public:
Fraction(void);
Fraction(int n, int d);
void plus(Fraction second);
void minus(Fraction second);
void times(Fraction second);
void divide(Fraction second);
void reduce();
double todecimal();
void scan(istream& in);
void print(ostream& out);
~Fraction();

private:
int num,den;

};

// Fraction.cpp
#include "Fraction.h"

Fraction::Fraction()
{
num=0;
den=1;
}


Fraction::Fraction(int n, int d){
num=n;
den=d;
}

void Fraction::plus(Fraction second){
int tempden= den*second.den;
num=num*second.den + den*second.num;
den=tempden;
}

void Fraction::minus(Fraction second){
int tempden= den*second.den;
num=num*second.den - den*second.num;
den = tempden;}

void Fraction::times(Fraction second){
num = num*second.num;
den=second.den*den;
}

void Fraction::divide(Fraction second){
num = num*second.den;
den= den*second.num;
}

void Fraction::scan(istream & in){
char slash;
in >> num >> slash >> den;

}


void Fraction::print(ostream & out){
out << num <<" / " << den;}

double Fraction::todecimal()
{double dec;

dec=(num*1.00)/(den*1.00);
return dec;
}

void Fraction::reduce()

{

}

Fraction::~Fraction()
{
}

//main.cpp

#include "Fraction.h"
using namespace std;
int main(){
Fraction first;
Fraction d(1,2);
cout << " Fraction class menu" << endl;
cout << " " << endl;
cout << " Please enter fraction in form of 1/2"<> choice;
switch (choice)
{
case 1: {
cout << "Enter a second fraction to be added to";
d.scan(cin);
first.plus(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();


break;}
case 2: {
cout << " Enter a second fraction to be subtracted from";
d.scan(cin);
first.minus(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
case 3: {
cout << "Enter a second fraction to be timed by" << endl;
d.scan(cin);
first.times(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
case 4:{ cout << "Enter a second fraction to be divided by" <

unikiller
Newbie Poster
5 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

nevermind got it finally

unikiller
Newbie Poster
5 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Ok ... nevermind ... don't got it ....

This is what I put

void Fraction::reduce()

{
int b=den;
int c;
int a=num;
while (b!=0){
c= a%b;
a=b;
b=c;
}

num = a/num;
den=a;

}


but I put 1/2 + 1/2 , and it should give me 4/4 or 1 , but it's giving me 1/4 or .25 .... weird, can anyone help me out ? 8)

unikiller
Newbie Poster
5 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

nevermind finally got it :)

unikiller
Newbie Poster
5 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Ok ... nevermind ... don't got it ....

This is what I put

void Fraction::reduce() { int b=den; int c; int a=num; while (b!=0){ c= a%b; a=b; b=c; }

num = a/num; den=a; }

but I put 1/2 + 1/2 , and it should give me 4/4 or 1 , but it's giving me 1/4 or .25 .... weird, can anyone help me out ? 8)


change the lines :

num = a/num;
den=a;

to
num = num/a;
den= den/a;


I hope it helps

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You