"Luis" <ki***@comcast.net> wrote in message

news:T4*******************@rwcrnsc52.ops.asp.att.n et...

can some one fix this so it actually reduces a fraction, and actually
works? I get a windows error when i try to run it.

http://www.rafb.net/paste/results/wS672281.html

Your code is confused, it can't be easily fixed. Look at the code for GCF

int Fraction :: GCF()

{

int remainder = 1;

int gcf;

while(remainder!=0)

{

remainder = denominator%numerator;

denominator = numerator;

numerator = remainder;

}

gcf = denominator;

return gcf;

}

Why is GCF a member of Fraction? What does calculating a GCF have to do with

fractions? More to the point why does GCF change the numerator and

denominator? Its the job of Reduce to change the numerator and denominator

by dividing by the GCF. Its not the job of GCF to do that. As I said you

are confused.

First job, write this function

int GCF(int x, int y)

{

// code to calculate GCF of x and y

return gcf;

}

Not this function is not a member of Fraction, its doesn't have anything to

do with Fractions, all it does it calculate a GCF. This is a good rule for

programming; one function, one job.

Now look at Reduce

Fraction Fraction :: Reduce ()

{

Fraction reducedFraction;

reducedFraction.set(numerator/GCF(),denominator/GCF());

return reducedFraction;

}

You've written Reduce to return a new Fraction, not reduce the existing one.

That's not wrong but its a bit strange. Its seems much more logical to

reduce the existing fraction When I look at DividedBy I see more evidence of

this confusion.

Fraction Fraction :: DividedBy (Fraction otherFraction)

{

Fraction reducedFraction;

Fraction quotient(numerator * otherFraction.denominator,

denominator * otherFraction.numerator);

Reduce();

return quotient;

}

Which fraction to do think is being reduced by the call to Reduce here?

reducedFraction? quotient? this? The answer is none of them! You wrote

Reduce to create a new Fraction which it returns, but when you call Reduce

here, you throw the return value away! You call to Reduce does nothing at

all (apart from waste some time).

Lets rewrite reduce so that it reduces the existing fraction, not returns a

new one, also let is use the GCF function I said you should write above.

void Fraction :: Reduce ()

{

int gcf = GCF(numerator, denominator);

numerator /= gcf;

denominator /= gcf;

}

See how it deivdes the existing Fractions numerator and denominator, its

doesn't try to create a new Fraction. The return type is void.

Now let rewrite DividedBy to use the new Reduce

Fraction Fraction :: DividedBy (Fraction otherFraction)

{

Fraction quotient(numerator * otherFraction.denominator,

denominator * otherFraction.numerator);

quotient.Reduce();

return quotient;

}

See how I call quotient.Reduce() to reduce quotient, simple!

Carry on in this vein, and you'll be getting closer. Its very important to

think clearly about you program design, you cannot throw together a few

functions that look roughly right and expect to get anything to work.

john