I'm having great difficulty with the following that I need to do for an assign:
a. Declare a data structure that contains a rational number.
b. Write f'xns that will +, -, *, / rational numbers.
All f'xns have to pass 3 parameters, each pointing to a data structure of the type I declared in part a; 2 of the parameters = operands, 3rd = result.
c. Write a f'xn that takes a pointer to your data structure as a parameter and returns the GCD of the numer. & denom.
d. Use your f'xn from part c to write a f'xn that will reduce a fraction (rational number) to lowest terms. Pass in a pointer to the fraction and have the fraction modified by the f'xn.
e. Write input and output functions so that a user can enter a fraction in the form 1/5 for example.
The user should be allowed to enter any number of problems, and the program should output the answer in lowest terms.
Am I on the right track? I believe I have a-c down, but not d and especially e. Can someone please guide me or help me correct my script?
int GCD (int numer, int denom)
{
int result;
while (denom > 0) {
result = numer % denom;
numer = denom;
denom = result;
}
return numer;
}
int getLCM (int numer, int denom)
{
int max;
max = (numer > denom) ? numer : denom;
while (1) {
if (max % numer == 0 && max % denom == 0)
break;
++max;
}
return max;
}
struct Fraction
{
int numer;
int denom;
};
typedef struct
{
int numer;
int denom;
};
Fraction
Fraction add_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
Fraction subtract_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
Fraction multiply_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.denom * b.denom);
sum.denom = (a.numer * b.numer);
return sum;
}
Fraction divide_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.denom * b.numer);
sum.denom = (a.numer * b.denom);
return sum;
}
int main ()
{
char response;
printf ("FRACTION ARITHMETIC PROGRAM\n");
printf ("Enter your problem (example 2/3 + 1/5):\n");
scanf (, &problem);
if (denom == 0 || denom < 0) {
printf ("Illegal input!!\n");
printf ("Another problem (y/n)? ");
scanf ("%c%*c", &response);
} else {
printf ("The answer is ");
printf ("Another problem (y/n)? ");
scanf ("%c%*c", &response);
}
while ((response == 'y') || (response == 'Y')) {
printf ("\nWould you like to play again?\n");
scanf ("%c%*c", &response);
}
while ((response == 'n') || (response == 'N'))
printf ("Goodbye and thank you");
return 0;
}
Edit after removing typedef thanks to comment responses:
struct Fraction {
int numer;
int denom;
};
struct Fraction add_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
struct Fraction subtract_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
struct Fraction multiply_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.denom * b.denom);
sum.denom = (a.numer * b.numer);
return sum;
}
struct Fraction divide_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.denom * b.numer);
sum.denom = (a.numer * b.denom);
return sum;
}
struct Fraction
is wholly unrelated to the typeFraction
, which is an anonymous (untagged) structure type with a typedef name ofFraction
. You should probably combine the two, though either one could be used. What's erroneous is having both. – MachismogetLCM
function. It would be a good idea to normalize fraction before returning by diving numer and denom with gcd. (Write a function to normalize) Eithertypedef
orstruct Fraction
can be removed to avoid confusion. – Bradfordbradleevoid
, though it could be a success/failure indicator showing whether the result could be stored). Your functions implement a more useful and usable scheme where the parameters are passed by value and the result is returned by value. However, that's not the spec. Beware of nitpicking teachers. You have similar issues with your GCD function; the specification says one interface, but you have another. – Machismo{ { }...
), but the original content was preserved. – Bolusint
can handle. Yet the final result is 1/50000 which is representable in your implementation. – Season