I am building a class fraction. I have it set up to take in two parameters such as fraction f1(4/10); and reducing it would be 2/5.
I need to write a simplify function that will simplify the fraction to its smallest simplification. my number keeps outputting 0/1 (which is my default constructor for when denominator is 0.)
1 2 3 4 5 6 7 8 9 10 11
void fraction::reduce()
{
for(int i = numer * denom; i > 1; i--)
{
if((numer % i) == 0 && (denom % i) == 0)
{
numer = numer / i;
denom = denom / i;
}
}
}
If anyone could help me out it would be greatly appreciated!
I thought it should too, but it doesn't. I have a constructor with a boolean setvalue function that I called inside it that basically says if you have a fraction with a denominator that is zero, then automatically make the fraction 0/1. So this is what my input/output is:
1 2 3 4 5 6
fraction f(20,40) //declaring fraction object
cout << f << " simplified is ";
f.simplify(); //simplify function call
cout << f << '\n';
//outputs: 0/1 simplified is 0/1
never mind I figured it out. It does work, I just had put in my original fraction f(20/40) instead of f(20,40) haha oops. Now it works perfect, thanks!