Hi,

This is the first time ever I post something about any programming language. Im just starting this semester with C language, so Im really new on this. This C code is a project we have for the class. We have to supply definitions for different functions. I have gone over all of them and found the program to work as expected, but for some reason the ReducedFraction function is not working. This function was given. I tried to use the function when calling any of the specified operations (+, -, *, /), but it doesn't reduce the fraction. I still need to work on a couple of operations, but that I know I can do with no problems, so if you see some of the code incomplete, I'm just leaving that part of the code for the end (specifically the switch statement). I don't expect you giving me the answer, just to get your feedback. Otherwise thank you anyways.

Here is the code:

/* Begin DO_NOT_MODIFY_REGION */

```
#include <stdio.h>
#include <stdlib.h> /* Provides the abs function */
#include <string.h>
#include <ctype.h>
typedef struct fraction {
int numerator;
int denominator;
} Fraction;
int gcd(int x, int y);
/* Precondition: a and b must be positive */
Fraction ReduceFraction(Fraction R);
Fraction inputFraction();
/* Prompts for, inputs and returns a single fraction */
Fraction AddFractions(Fraction R1, Fraction R2);
/* Returns R1+R2 in reduced form */
Fraction SubtractFractions(Fraction R1, Fraction R2);
/* Returns R1-R2 in reduced form */
Fraction MultiplyFractions(Fraction R1, Fraction R2);
/* Returns R1*R2 in reduced form */
Fraction DivideFractions(Fraction R1, Fraction R2);
/* Precondition: in reduced form R2 must be nonzero
Returns R1/R2 */
/* NOTE: IN THE NEXT TWO FUNCTIONS, THE LOGICAL TYPE OF THE op PARAMETER
IS char; HOWEVER, IT IS DECLARED AS int TO AVOID SOME ARCANE
COMPILER WARNINGS. FUNCTIONS IN THE STANDARD LIBRARY OFTEN
DECLARE PARAMETER OR RETURN TYPE int WHERE char WOULD BE EXPECTED */
void PrintResult(Fraction R1, Fraction R2,int op);
/* Carries out the operation indicated by op and prints
a string showing the two operand, the operator and the result */
void GetAndDoFractionOp(int op);
/* Prompts for two fractions
then carries out the specified operation and prints
the result. In the case of division, checks to see
if the second fraction is zero; if so, continues
prompting for and inputing fractions until a nonzero
fraction is input */
int main()
{
char response, theOp;
do {
printf("\n\n-----------------------------------------");
printf("\n\nEnter the operator you want to apply (+,-,*,/)\n");
scanf(" %c",&theOp);
while(getchar() != '\n');
while( theOp != '+' && theOp != '-' && theOp != '*' && theOp != '/')
{
printf("%c is not a valid operator; try again\n",theOp);
scanf(" %c",&theOp);
while(getchar() != '\n');
}
GetAndDoFractionOp(theOp);
printf("Do you wish to do another (Y/N)?");
scanf(" %c",&response);
} while(toupper(response) == 'Y');
printf("Normal termination\n\n");
return 0;
}
int gcd(int x, int y)
{
if (x%y == 0)
return y;
return gcd(y,x%y);
}
Fraction ReduceFraction(Fraction R)
{
int d;
if (R.numerator == 0)
{
R.denominator = 1;
return R;
}
d = gcd(abs(R.numerator),abs(R.denominator));
R.numerator /= d;
R.denominator /= d;
if (R.denominator < 0)
{
R.numerator *= -1;
R.denominator *= -1;
}
return R;
}
/* End DO_NOT_MODIFY_REGION */
/* You supply the definitions for the remaining functions */
Fraction InputFraction()
/* No prompts; just input the fraction */
{
Fraction single_frac;
scanf("%d/%d", &single_frac.numerator, &single_frac.denominator);
return single_frac;
}
Fraction AddFractions(Fraction R1, Fraction R2)
{
Fraction reduced;
printf("%d/", reduced.numerator = R1.numerator*R2.denominator+R2.numerator*R1.denominator);
printf("%d", reduced.denominator = R1.denominator*R2.denominator);
return reduced;
}
Fraction SubtractFractions(Fraction R1, Fraction R2)
{
Fraction reduced;
printf("%d/", reduced.numerator = R1.numerator*R2.denominator-R2.numerator*R1.denominator);
printf("%d", reduced.denominator = R1.denominator*R2.denominator);
return reduced;
}
Fraction MultiplyFractions(Fraction R1, Fraction R2)
{
Fraction reduced;
printf("%d/", reduced.numerator = R1.numerator*R2.numerator);
printf("%d", reduced.denominator = R1.denominator*R2.denominator);
return reduced;
}
Fraction DivideFractions(Fraction R1, Fraction R2)
{
Fraction reduced;
printf("%d/", reduced.numerator = R1.numerator*R2.denominator);
printf("%d", reduced.denominator = R1.denominator*R2.numerator);
return reduced;
}
void PrintResult(Fraction R1, Fraction R2, int op) /* you should use op as a char object in your code */
{
switch (op)
{
case '+':
printf("(%d/%d) + (%d/%d) = ", R1.numerator, R1.denominator,
R2.numerator, R2.denominator);
AddFractions(R1, R2);
printf("\n");
break;
case '-':
printf("(%d/%d) - (%d/%d) = ", R1.numerator, R1.denominator,
R2.numerator, R2.denominator);
ReduceFraction(SubtractFractions(R1, R2));
printf("\n");
break;
}
}
void GetAndDoFractionOp(int op) /* you should use op as a char object in your code */
{
Fraction Frac1, Frac2;
printf("Please enter the first fraction argument (n/d)\n");
Frac1 = InputFraction();
printf("Please enter the second fraction argument (n/d)\n");
Frac2 = InputFraction();
switch (op)
{
case '+':
PrintResult(Frac1, Frac2, op);
break;
case '-':
PrintResult(Frac1, Frac2, op);
break;
}
}
```