I am having a hard here and could use some help (I have been at this for hours and am getting no where...). OK, so my problem is that I can't figure out how to make a Function that uses a typedef to return a simplified fraction. In other words I want to use Euclidean method to get GCD like this:
int gcd(int a, int b)
{
int rem;
if ((rem = a % b) == 0) {
return b;
}
else {
return gcd(b, rem);
}
}
Then simply with something like this:
int result = gcd(num, den);
int simple_num = num / result;
int simple_den = den / result;
printf("%d / %d", simple_num, simple_den);
But, I am trying to make it with a typedef called fraction and use one function that will return my simplified numerator and denominator.
typedef struct
{
int numerator;
int denominator;
} Fraction;
Fraction simplify(Fraction myFraction)
{
return myFraction;
}
Anyone know how this could be done?
This should work:
Fraction simplify(Fraction myFraction)
{
int result = gcd(myFraction.numerator, myFraction.denominator);
int simple_num = num / result;
int simple_den = den / result;
Fraction newFraction = {simple_num, simple_den};
return newFraction;
}